C# Interview Questions And Answers

If you need to know other interview questions and answers, I strongly recommend to read my following articles:

Important ADO.NET Interview Questions
SQL Interview Questions And Answers

Now in this post we are going to share the interview questions for C# or a Dot Net developer. No matter you are an experienced professional or a fresher, it is important that you must be aware of these questions. So please read it carefully. I hope you will like this article.

Background

C# is one of my favorite programming language. So I am really happy to share you some interview questions related to C#. So shall we now discuss about C# interview questions?

Question: What are Abstract Classes?

The purpose of an abstract class is to define some common behavior that can be inherited by multiple subclasses, without implementing the entire class. In C#, the abstract keyword designates both an abstract class and a pure virtual method.

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Properties

  • An abstract class cannot be instantiated.
  • It can be inherited.
  • It can have method implementations and class members.
  • Only abstract classes can have abstract methods.

Syntax

public abstract class A
{
   public abstract void DoWork(int i);
}


Question: What are Abstract methods? Give an example?

Abstract Methods

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. (See above example for syntax).

Derived classes of the abstract class must implement all abstract methods.

When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.

Example

  1. // compile with: /target:library   
  2. public  class D  
  3. {   
  4.     public  virtual  void DoWork(int i)   
  5.     {  
  6.         // Original implementation.  
  7.     }   
  8. }  
  1. public  abstract  class E: D  
  2. {  
  3.     public  abstract  override  void DoWork(int i);  
  4. }  
  5. public  class F: E  
  6. {  
  7.     public  override  void DoWork(int i)   
  8.     {  
  9.        // New implementation.  
  10.     }  
  11. }  
Question: What is Interface? Explain with an example?

An interface is useful when you want to use some common functionality of otherwise unrelated classes- they share no implementation details, only the function signatures. In C#, function declarations within an interface are implicitly pure virtual.

An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. 

Properties

An interface can be a member of a namespace or a class and can contain signatures of methods, properties, events, indexers, etc.

An interface can inherit from one or more base interfaces.

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

Example
  1. interface IControl   
  2. {   
  3.     void Paint();   
  4. }  
  5. interface ISurface   
  6. {  
  7.     void Paint();   
  8. }  
  1. public  class SampleClass: IControl, ISurface   
  2. {  
  3.     void IControl.Paint()   
  4.     {  
  5.         System.Console.WriteLine("IControl.Paint");   
  6.     }  
  7.     void ISurface.Paint()   
  8.     {  
  9.         System.Console.WriteLine("ISurface.Paint");   
  10.     }    
  11. }  
Calling the methods
  1. // Call the Paint methods from Main.  
  2. SampleClass obj = new SampleClass();  
  3. //obj.Paint(); // Compiler error.  
  4.   
  5. IControl c = (IControl)obj; c.Paint();  
  6. // Calls IControl.Paint on SampleClass.  
  7.   
  8. ISurface s = (ISurface)obj; s.Paint();  
  9. // Calls ISurface.Paint on SampleClass.   
Question: Explain The Difference Between Abstract Class and Interface ?

An Abstract class do not provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.

Using Abstract we cannot achieve multiple inheritance but using an Interface we can achieve multiple inheritance.

We cannot declare a member field in an Interface.

We cannot use any access modifier i.e. public, private, protected, internal, etc. because within an interface by default everything is public.

An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.

Question: Explain Generic Collections & Array Lists ?

Generic allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generic allow you to write a class or method that can work with any data type.

Generic Collections helps us to create flexible type-safe, strong type collections at compile time.

Syntax

List<int> myInts = new List<int>();

Namespace
  1. System.Collections.Generic  
ArrayList

They are ordered collection of objects, that can be resized automatically, that has dynamic memory allocation and which can contain different types of data.

Arraylist stores its members as objects, so in order to retrieve it we must type cast it.

Syntax and Example
  1. ArrayList intlist = new ArrayList();  
  2. intlist.Add(34);  
  3. intlist.Add("sasi");  
Namespace
  1. System.Collections;  
Question: What are Finalize and Dispose ? Can you list down the differences between them?

Finalizers are run by the Garbage Collection before an object that is eligible for collection is reclaimed. Dispose() is meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, etc. It works best in conjunction with the using block where the compiler makes sure that Dispose() will be called immediately once you are done with an object – and also ensures that you cannot work with the object any more once it’s disposed.

Dispose() Method
  • This dispose method will be used to free unmanaged resources like files, database connection, etc.
  • To clear unmanaged resources we need to write code manually to raise dispose() method.
  • This Dispose() method belongs to IDisposable interface.
  • If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.
  • It will not show any effect on performance of website and we can use this method whenever we want to free objects immediately.

Finalize() Method

  • This method also free unmanaged resources like database connections, files, etc.
  • It is automatically raised by garbage collection mechanism whenever the object goes out of scope.
  • This method belongs to object class.
  • We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process is done.
  • It will show effect on performance of website and it will not be suitable to free objects immediately.

Question: What is Dependency Injection?How can we implement it?

Simply put Dependency injection is for decoupling two components. It can be explained by a simple example.

  1. public class ErrorLogger  
  2. {  
  3.     public void WriteIntoDataBase(string message)  
  4.     {  
  5.         // Creates DataBase Entry.  
  6.     }  
  7. }  
  8. public class ApplicationWatcher  
  9. {  
  10.     ErrorLogger logger = new ErrorLogger();  
  11.     public void Notify()  
  12.     {  
  13.         logger.WriteIntoDataBase("This is the message that I want to write.")  
  14.     }  
  15. }  
Have a look at the code above.

I have an ErrorLogger class that writes an error in to database. The method is actually called from another class ApplicationWatcher.

In a later stage if I want to send an email instead of writing it to database, this design will not suffice. I will have to create another class that has a method that sends email and create its instance in the ApplicationWatcher.

Conclusion

Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

If there is any question, then please mention it in the comments section.
 
Read this article in my blog here.
 
You can enhance your knowledge more, by reading the following articles.
 

Up Next
    Ebook Download
    View all
    Learn
    View all