C# Interview Questions and Answers

C# interview questions

These C# questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on C#.

Who is this C# interview questions designed for?

All the developers, programmers and software engineers will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in Software Development will be highly benefited by these questions.

C# interview questions topics

This section covers C# topics like - features of C#, Managed and Unmanaged Code, struct and class, Boxing and Unboxing, overloading and overriding, DLL Hell problem etc.

1. What are the features of C#?

Some of the features of C# are:

  • C# is a powerful, simple and pure object-oriented programming language.
  • C# has the feature of versioning which means different versions of assemblies can exist side by side.
  • C# language is a type safe language which means C# code accesses only the memory locations it is authorized to access.
  • C# has rich library with many inbuilt functions for easy and fast development.
  • C# supports component-oriented programming.
  • C# can develop iOS, Android, and Windows Phone native apps using Xamarin Framework.

2. What is Managed and Unmanaged Code?

The code which is developed in .NET framework and whose execution is managed by Common Language Runtime or CLR is known as managed code. It is CLR that compiles managed code into machine code and then executes it.

The code which is developed outside .NET framework and does not run under the control of the CLR is known as unmanaged code. For example, PowerPoint and Microsoft Excel do not require CLR, they run under their own environment.

3. What is the difference between a struct and a class in C#?

Class and struct are both user-defined data types. Difference between Struct and Class are:

  • The struct is a value type in C# and is stored on the stack whereas the class is a reference type in C# and is stored on the heap.
  • Struct is usually used for smaller amounts of data, but Class is usually used for large amounts of data.
  • Struct can’t be inherited from other types. Classes can be inherited from other classes.
  • A structure can't be abstract, but a class can be an abstract type.
  • A struct can't have destructors, but a class can have destructors.

4. What is Boxing and Unboxing in C#?

The process of implicit conversion from a value type to a reference type is called boxing.

The process of explicit conversion from a reference type to a value type is called unboxing.

Example

int MyVal = 18;  

// Boxing
Object objBoxed = MyVal; //implicit Boxing

// Unboxing
int MyValUnBoxed = (int) objBoxed;

5. What is the difference between overloading and overriding?

Overloading allows a class to have more than one method having the same name but different signatures.

//Overloading
public class myClass
{
    public void getName(int id)
    {}
    public void getName(string name)
    {}
}

Overriding allows changing the functionality of a method in a child class. With overriding, inheriting classes can change how we expect a class to behave.

//Overriding
public class myClass
{
        public virtual void getName(int id)
        {
              //Get Name
        }
}
public class myClass1 : myClass
{
        public override void getName(int id)
        {
            //base.getName(id);
            //Get present address
        }
}

6. How encapsulation is implemented in C#?

Encapsulation is one of the fundamental concepts in OOP. It is the process of wrapping data members and member functions into a single unit called class. We can implement encapsulation by using access specifiers. An access specifier defines the scope and visibility of a class member.

Public access specifier allows a class to expose its member variables and member functions from outside the class scope. Public member can be accessed from anywhere within the program.

Private access specifier allows a class to hide its member variables and member functions from other member functions or class objects. The private members of a class can be accessed by only the functions belonging to the same class.

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.

7. What is a preprocessor directives in C#?

The preprocessor directives tell the compiler to preprocess the information before actual compilation starts.

All preprocessor directives begin with a hashtag symbol (#) and since these preprocessors are not statements so no semi-colon is appended at the end.

8. How C# supports dynamic polymorphism?

Polymorphism is one of the important pillars of object-oriented programming just like Encapsulation and Inheritance. Polymorphism means many forms. It gives the ability to a class to have multiple definitions for a method with the same name. It is of two types:  Static Polymorphism and Dynamic Polymorphism.

The static polymorphism is often referred to as compile-time or early binding polymorphism. It is implemented using method overloading and operator overloading.

The dynamic polymorphism is referred to as run-time or late binding polymorphism and it is implemented using method overriding.

9. What is early binding?

The mechanism of linking an object with their functionality at the compile-time is called early binding. It is also called static binding.

10. What is the purpose of using statement in C#?

We use using keyword to include a namespace in the program. A program generally can have multiple using statements.

11.  How is the DLL Hell problem solved in .NET?

Earlier, before the release of .NET, DLL hell problem was quite common. It was caused when multiple applications attempt to share a common dll. When you used to install a new application, a new dll of that application used to replace the older one in the registry that was not backward compatible. It was Ok for new application, but the existing application that was depended on previous version of dll used to crash. This was because the version information of different components of an application was not recorded by the system.   

This problem is solved in .NET with the introduction of Assembly versioning. It allows the application to specify not only the library it needs to run, but also the version of the assembly.

12. How to prevent your class from being inherited?

You can use keyword 'sealed' in the class definition to prevent class from being inherited.

13. When do we declare a class as abstract in C#?

Abstraction means showing only essential information to the user. We use abstract class to achieve Abstraction in C#. An abstract class cannot be instantiated, it is designed to be inherited by subclasses. An Abstract Class is used for a large project to share the common functionality with its derived class.

14. What is the difference between an abstract class and interface?

An abstract class can have implementations for some of its members, but the interface can't have implementation of any of its members.

Abstract class members can have access modifiers whereas interface members can't have access modifiers.

15. What does a generic class mean?

Generic class allows us to design classes or objects that don’t have a specific data type. The data type is assigned during the runtime.

16. What is the advantage of using StringBuilder over String?

StringBuilder is the mutable string type. If we perform any operation on StringBuilder, it will update the existing instance value and it will not create new instance. Strings are immutable, so each time it's being operated on, a new instance is created.

StringBuilder is more efficient in the situations where you need to perform repeated modifications to a string.

17. What is Jagged Arrays?

The Array which has elements of type array is called jagged Array. The elements can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays".

18. Describe the accessibility modifier "protected internal".

Protected Internal is a combination of a protected and an internal access modifier. The Protected Internal members can be accessed by any code in the assembly in which it is declared, as well as by any class that inherits it, regardless of the assembly.

19. What are Custom Control and User Control?

Custom Controls are compiled code components (dll files) that execute on the server. They are easier to use and can be added to toolbox. Developers can drag and drop controls to their web forms.

User Controls are easy to create and reusable controls. They are very much similar to ASP include files. User controls can't be placed in the toolbox and dragged - dropped from it. They have their design and code-behind. The file extension for user controls is ascx.

User Control, being a page file can only be used within a single application or project, but custom controls are assemblies (dll files) that can be used in multiple applications.

20. What is object pool in .Net?

Object pool is a container of ready to use objects. Whenever a new request comes in for creating an object, then the application is served from this pool. This reduces the overhead of creating an object over and over again.