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.

What needs to be done for a class to be used inside a using block

To enable a class to be used under a using() block, the class must implement the IDisposable interface with its own implementation of the Dispose() method.

public class MyClass : IDisposable {
    public void Dispose() {
      // implementation of Dispose method
    }
}

// in some other method
using(var myclass = new MyClass()) {
    // myclass instance is alive inside 
    // this block only
}

Dispose is never called by the .NET Framework; you must call it manually - preferably by wrapping its creation in a using() block.