Interview Questions C#

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

C# Interview Questions

1. What is the difference between ref and out keywords?

ref keyword is used to pass an already initialized variable to a method as a reference type,
facilitating bi-directional data passing.
Whereas out keyword is used to pass a variable as an empty container that can store multiple
values to a method as a reference type. out keyword allows uni-directional data passing, as
the container passed using out keyword doesn’t need to be initialized beforehand.

2. write a code on Boxing and Unboxing in C#?


The methods of boxing and unboxing are used for type conversions. Boxing is converting from a
value type to a reference type. Boxing is an implicit conversion. Here's an example of C# boxing.
// Boxing
int num1 = 1;
Object obj = num1;
Console.WriteLine(num1);
Console.WriteLine(obj);

Unboxing is the process of converting from a reference type to a value type. Here's a C#
example of unboxing.
// Unboxing
Object obj2 = 1;
int num2 = (int)obj;
Console.WriteLine(num2);
Console.WriteLine(obj);

3. What are the distinctions between Array and Array List in C#?
Arrays store values or elements of the same data type, whereas array lists store values of
various data types.
Arrays will use a fixed length, whereas array lists will not.

4. In C#, what are partial classes?


Partially implemented classes distribute the functionality of a single class across multiple files.
During the compilation process, these multiple files are combined into one. The partial
keyword is used to create the partial class.
Methods, interfaces, and structures' functionalities can be easily separated into multiple files.
You can also include nested partial classes.

Can different assembly Partial classes combine into one? – No

5. Can “this” be used within a static method?


We can't use 'this' in a static method because the keyword 'this' returns a reference to the
current instance of the class containing it. Static methods (or any static member) do not
belong to a particular instance. They exist without creating an instance of the class and are
called with the name of a class, not by instance, so we can’t use this keyword in the body of
static Methods. However, in the case of Extension Methods, we can use the parameters of the
method.

Explain “this” keyword.


The "this" keyword in C# is a special type of reference variable implicitly defined within each
constructor and non-static method as the first parameter of the type of class in which it is
defined.

6. What is Constructor Chaining in C#?


Constructor chaining is a way to connect two or more classes in a relationship as Inheritance.
In Constructor Chaining, every child class constructor is mapped to a parent class Constructor
implicitly by the base keyword, so when you create an instance of the child class, it will call
the parent’s class Constructor. Without it, inheritance is not possible.

7. Can we override a constructor?


No, in C#, it is necessary to define properly which constructor you are trying to call to
instantiate a class and what arguments are being passed. So you cannot override a constructor
in C#.

8. What is Serialization in C#?


When we want to store any object to a memory, a database, or a file, it needs a special
process known as Serialization.
Serialization is the process of converting an object into a different form to store it on to a file,
database, or memory. The purpose of Serialization is to transfer the object and its state across
the network and recreate it successfully when needed.
The reverse of Serialization is known as Deserialization.
There are many types of serialization in C#, such as:
 Binary serialization: To save the state of the object in binary format. This is done using
classes defined in the System.Runtime.Serialization namespace.
 Soap Serialization: To save the state of the object in binary format, with the use of
network-related communication.
 XML Serialization: To save the state of the object in XML format. This is done using
classes defined in the System.Xml.Serialization namespace.
 JSON Serialization: To save the state of the object in JSON format. This is done using
classes defined in the System.Text.Json namespace.

9. What is reflection in C#?


During runtime, C# reflection extracts metadata from data types.
To implement reflection in the.NET framework, simply use the System.Reflection namespace
in your program to retrieve the type, which can be any of the following:
 Assembly
 ConstructorInfo
 MemberInfo
 ParameterInfo
 FieldInfo
 EventInfo
 PropertyInfo
10. How do you use the “using” statement in C#?
There are two ways to use the using keyword in C#. One is as a directive, and the other is as a
statement.
 using Directive
Generally, we use the using keyword to add namespaces in code-behind and class files. Then it
makes available all the classes, interfaces, and abstract classes and their methods and
properties on the current page. E.g., using System.Web;
 Using Statement
This is another way to use the keyword in C#. It plays a vital role in improving performance in
Garbage Collection. It will be used in declaring variables and its scope.
e.g. using(ClassA a = new ClassA())
{
// Scope of instance ‘a’ of the ClassA
}

You might also like