C# Interview Questions Part 2


1)  What is the different between “ref” and “out” parameters?

There is no difference between ref and out parameters. The only difference is that the ref input parameters need an input value and the out parameters don’t

2) What operators can be used to cast from
one reference type to another without the risk of throwing an exception?

The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception.

3)  What is boxing and unboxing?

Boxing - Converting a value type to reference type is called
boxing. An example is shown below.

int a = 5;

object obj = (object)a; // Boxing

Unboxing - Converting a reference type to a value type is called
unboxing. An example is shown below.

obj = 5;

a = (int)obj; // Unboxing

4)  Is boxing an implicit conversion?
             Yes, boxing happens implicitly.

5)  What happens during the process of boxing?
Boxing is used to store value types in the garbage-collected heap memory. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.

6) Arearrays value types or reference types?

Arrays are reference types.

7)What do you mean by String objects are immutable?

String objects are immutable means; they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the, following example, when the contents of s1 and s2 are concatenated to form a
single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

string s1 = “StringA “;

string s2 = ” StringB”;

// Concatenate s1 and s2. This actually creates a new

// string object and stores it in s1, releasing the

// reference to the original object.

s1 += s2;

System.Console.WriteLine (s1);

// Output: First String Second String

8)What is the difference between int.Parse and int.TryParse methods?

Parse method throws an exception if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse.

9)What is the difference between System.Text.StringBuilder and System.String?

  1. Objects of type StringBuilder are mutable where as objects of type System.String is immutable.
  2. As StringBuilder objects are mutable, they offer better performance than string objects of type System.String.
  3. StringBuilder class is present in System.Text namespace where String class is present in System namespace.

10) What is the difference between an implicit conversion and an explicit conversion?

  1. Explicit conversions require a cast operatorwhere as an implicit conversion is done automatically.
  2. Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.

11) What are the differences between value types and reference types?

  1. Value types are stored on the stack where as reference types are stored on the managed heap.
  2. Value type variables directly contain their values where as reference variables hold only a reference to the location of the object that is created on the managed heap.
  3. There is no heap allocation or garbage collection overhead for value-type variables. As reference types are stored on the managed heap, they have the over head of object allocatio and garbage collection.
  4. Value Types cannot inherit from another class or struct. Value types can only inherit from interfaces. Reference types can inherit from another class or interface.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.