What are the differences between an Array and an ArrayList?

Arrays:

  • An Array uses a vector array for storing data
  • It can store only a single data type
  • Length of the Array is fixed and can't be increased dynamically
  • since the type is specified during declaration, there's no need for type casting the values

ArrayList:

  • An ArrayList is of variable length which can accommodate any number of elements
  • It uses a LinkedList for storing data
  • It is a dynamic type which can store any datatype
  • since it can store any type of data, it requires type casting while accessing the values

What are Jagged Arrays?

An array of arrays is called a Jagged Array. Each individual array within the Jagged Array can be of variable length.

var jaggedArray = new int[10][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[10];
jaggedArray[2] = new int[2];

What is meant by Boxing and Unboxing?

The method of converting a value type into a reference type is called as BOXING

int a = 15;
string myString = a.ToString();

The conversion of a reference type into a value type is called as UNBOXING

string str = "15";
int val = int.parse(str);

What are sealed classes?

A sealed class is a type used to restrict users from extending the type. A type marked CANNOT be extended by inheritance, but can be referenced by an object.

public sealed MyClass {
  // some code
}

// throws compilation error
public class SomeClass : MyClass {
}

What are the different types of access modifiers present in C#?

These are the following access modifiers present in C#, which define how a component or a field can be accessed by the other components.

public - available for access by all other components inside or outside its class or assembly private - not available for access by any other component outside its class internal - available for access by other components only within its assembly protected - available for access by only its derived types protected internal - available for access by only its derived types or other components within its assembly

What is the difference between Managed Code and Unmanaged Code?

Managed Code refers to the components developed within the framework and which can be managed by the CLR for execution. These components can be managed by the framework itself and can be Garbage Collected.

Unmanaged Code refers to the components which are not developed within the framework and which need their own environment to be managed or executed. For example, C++ or assembly code which aren't managed or can't be garbage collected by the .NET framework directly.

These are called inside the Framework by means of wrappers.

How do you ensure that the Dictionary Keys are Unique in case of Complex Type Keys?

For example if we have a Dictionary defined as:

class MyClass { 
  public MyClass(int id) { 
    this.Id = id 
  }
  public int Id { get; set; }
}

Dictionary<MyClass, int> dict = new Dictionary<MyClass, int>();
dict.Add(new MyClass(1), 1);
dict.Add(new MyClass(2), 2);
dict.Add(new MyClass(3), 3);

To ensure that the Keys are always unique, we need to implement the GetHashCode() method for the MyClass which by default extends the System.Object class. This is the method that the Dictionary internally uses to compute HashKey for the Keys.