In this article, we will discuss the most asked C# interview questions and answers. If you need to know other interview questions and answers, I strongly recommend to follow this link: Interview Questions. Now in this post, we are going to share the interview questions for C# or a Dot Net developer. No matter you are experienced or fresher, it is important that you must aware of these. So please read it carefully. I hope you will like this article.
C# is one of my favorite programming languages. So I am really happy to share you some interview questions related to C#. You can always read my other interview questions here in the below links.
- Interview Questions For Experienced and Beginner .NET Professionals
- Infosys Interview Questions For DotNet Professionals
- SQL Interview Questions And Answers
- Important ADO.NET Interview Questions
So shall we now discuss C# interview questions?
C# Interview Questions
What are Abstract Classes?
The purpose of an abstract class is to define some common behavior that can be inherited by multiple sub classes, 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.
- An abstract class cannot be instantiated.
- It can be inherited.
- It can have method implementations, class members.
- Only abstract classes can have abstract methods.
What are Abstract methods?Give an example?
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.
What is Interface? Explain with an example?
An interface is useful when you want to be able 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.
An interface can be a member of a namespace or a class and can contain signatures of methods, properties, events, indexers.
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.
Calling the methods
Explain The Difference Between Abstract Class and Interface ?
An Abstract class doesn’t 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 inheritances but be using an Interface we can achieve multiple inheritances. 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.
Explain Generic Collections & Array Lists ?
Generics 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, generics 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.
They are ordered a 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
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, &c. 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 anymore once it’s disposed of.
- 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 the 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 the performance of the website and we can use this method whenever we want to free objects immediately.
- 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 was done.
- It will show the effect on the performance of the website and it will not suitable to free objects immediately.
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.
Have a look at the code above.
I have an ErrorLogger class that writes an error into database. The method is actually called from another class ApplicationWatcher.
In a later stage, if I want to send an email instead of writing into the database, this design will not suffice. I will have created another class that has a method that sends an email and creates its instance in the application watches.
What is Data Encapsulation and explain its Implementation?
To know about the Data encapsulation in C#, I recommend you to read here:
Data Encapsulation and Its Implementation
That’s all. Have a great day.
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?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.