Register Login

Top 25 C# Interview Questions and Answers

Updated Nov 20, 2018

What is C# (Sharp)?

C# is an object-oriented programming language that is both general-purpose and modern in its features. Developed by Microsoft, it was designed to be compatible with Common Language Infrastructure (CLI). C# consists of executable codes and a user-friendly runtime environment for different high-level languages across a wide range of computer architectures and platforms.

What are the different types of comments in C#?

C# features syntax for three types of comments — single-line, multiline and XML.

  • The multiline comments feature one/ more than one line of comments within a set of delimiters. 
  • Single-Line comments feature a single narrative in any given line at a time. 
  • XML Documentation comments or XML comments begin with a triple slash - ///. They are best utilized by IntelliSense features of Visual Studio as well as VS Code for depicting formation about specific types or members.

What is interface in C#?

An interface in C# syntactical contract within which all the classes that inherit the interface should follow by default. It shows the 'what' portion of the syntactical contract while the deriving classes depict the 'how' portion of the syntactical contract. The interface defines methods, properties and events –which are all interface members. An interface provides a standard structure to be followed by the deriving classes.

What is namespace in C#?

Namespace in C# useful for organizing various classes to make it convenient for users to handle the application with ease. In simple C# program, System. A Console is used, wherein System refers to the namespace and Console accounts for the class. The command namespacename.classname. can be used for accessing the class belonging to any namespace. 

What is abstract class in C#?

An abstract class in C# is a class designed for specific usage as a base class. It has one pure virtual function at least. 

What is constructor in C#?

Constructor refers to a special method in C# that’s automatically invoked when an object is created. This method is used for initializing data members of the newly created objects. A constructor in C# will possess the same name as struct or class. It is of two types. 

What is delegate in C#?

Delegates in C# are similar to the function pointers specific to C or C++. They allow for the encapsulation of a reference to any specific method within a delegate object. Once the process of encapsulation is done, the delegate object is ready to be passed to the code that in turn calls the referenced method. 

What is assembly in C#?

An assembly in C# features multiple modules. It is a file that can be automatically generated with the help of a compiler once each .NET application is successfully compiled. An assembly may either be in the form of an executable file or a Dynamic Link Library. Generated only for a single time for any specific application, an assembly is capable of getting updated with each subsequent compilation. 

What is ienumerable in C#?

IEnumerable in C# refers to a standard interface that supports iterating over collections. 

What is object in C#?

An object refers to a block of memory, which is allocated and configured as per a blueprint. Many objects belonging to the same class can be created by a program. Also called instances, objects may either be stored as named variables or within an array/collection.

How to convert a String to an Integer in C#?

The easiest method of converting a String to Integer value in C# is by making use of the int. Parse method. It returns an integer value as given below:

string words = "43";
int number = int.Parse("text");
Console.WriteLine(number);

How to append string in C#?

Concatenation refers to the method of appending (adding) a string to the end of any given string. Strings can be concatenated with the help of the + operator. The process of appending takes place at compile time (not run time) in case of string constants and string laterals. The concatenation of string variables takes place at run time only. 

Why is multiple inheritance not supported in C#?

C# limits classes to single inheritance only wherein each class will inherit from a singular parent class. As multiple inheritance may lead to ambiguity, it does not serve to be a feature of C#.

How can you use ternary operator in C#?

A ternary operator or conditional operator is used for checking a conduction. It works as an if statement and can also be used for the comparison of two values. A third value is produced by the ternary operator, the value of which is dependent of the result thrown back by the comparison. The C# compiler helps in translating ternary expressions to branch statements like brtrue at compile time. 

What is serialization in C#?

Serialization refers to the process of converting objects into data streams for the sake of storing objects or transmitting the same to memory, databases, or files. It mainly functions to save the current state of any object so that it may be recreated as and when needed. Deserialization is the process that is opposite to Serialization. 

How to deserialize JSON in C#?

Deserialization, which is opposite to Serialization, is used for converting JSON strings to custom .Net objects. The following code helps users create an instance of class BlogSite and assign different values to the properties exhibited by the same. Then, an instance of class DataContractJsonSerializer is generated by giving the parameter class BlogSite and generating an instance of class MemoryStream class for writing object(BlogSite). Lastly, an instance of class StreamReader is created for reading JSON data from object (MemorySteam).

string json = "{"Description":"Share Knowledge","Name":"C-sharpcorner"}";    
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))  
{  
   // Deserialization from JSON  
   DataContractJsonSerializer deserializer = new DataContractJsonSerializer (typeof (BlogSite));  
   BlogSite bsObj2 = (BlogSite) deserializer.ReadObject (ms);  
   Response.Write ("Name: " + bsObj2.Name); // Name: C-sharpcorner
   Response.Write ("Description: " + bsObj2.Description); // Description: Share Knowledge  
}  

How does garbage collector work in C#?

When any object is created in C#, then the CLR (common language runtime) is called in to assign memory to the object from heap. The GC (Garbage collector) accesses the heap to collect all the objects that are no longer useful for application and frees them all from the memory. 

What is Jagged Arrays in C#?

A jagged array or ‘array or arrays’ refers to array that comprises of more arrays as its elements. The elements belonging to a jagged array usually comprise of different sizes and dimensions. 

What is generics in C#?

Generics allows coders to define a class containing placeholders for the type belonging to its methods, parameters, fields, etc. These placeholders are replaced by Generics with specific type placeholders at compile time. Angle brackets are used for defining a generic class. 

What is LINQ in C#?

Language-Integrated Query or LINQ refers to a set of technologies that are based on the incorporation of query capabilities into C# language directly. Traditionally, the queries generated against data are in the form of simple strings and do not go through IntelliSense support or are type checked at compile time.

What is a sealed class in C#?

A sealed classes is used for restricting the inheritance feature belonging to object oriented programming languages.  In case any class has been defined as a sealed class then it is restricted from being inherited. The sealed modifier is responsible for defining a class as a sealed class in C#.

What is boxing and unboxing in C#?

Boxing refers to the converting of a value type to any interface type or type object implemented by the specific value type. When a value type is boxed by CLR, a value is wrapped by it into the system and stores it for later use on the managed heap. Unboxing refers to the process of extracting the value type so assigned from the object.

What is extension method in C#?

The extension method allows coders to add methods to the existing types. They can do so without creating new derived types, modifying original types, or recompiling the already created types. This method serves to be a specially designed, static method. It is called as an instance method on the extended type. 

How can the Extension Methods be implemented?

The static class, Extensions, features the extension methods that is defined for the specific type that will implement IMyInterface. The classes A , B and C are all capable of implementing the interface. When an instance method carrying a matching signature cannot be found by the compiler, then it binds itself to a matching extension method ione exists

How to call JavaScript function in C#?

For calling the JavaScript function with the help of Code Behind and without using ScriptManager, the coder has to use the RegisterStartupScript method belonging to the  ClientScript class of ASP.Net using C#.

How can a string be converted to datetime in C#?

Convert.ToDateTime(String)
The above method converts any specified string representation of time and date and time to its corresponding time and date value. Given below is the sample code for the conversion:
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + "  " + oDate.Year


×