C# Interview Question
C# Interview Question
When a member in C# has the Static access modifier in front of it, such as static
method, static variable, or static parameters, it means that the element has global
access and any other element can access it using class directly. You don't need to
create an instance of the class to access that element. The compiler stores the
address of the method as the entry point and uses this information to begin
execution before any objects are created.
A delegate is a reference type entity that is used to store the reference of a method.
So whenever you need to call a method, you can easily use the delegate by
initialising a pointer to that method, thereby helping to implement
Encapsulation.Delegates in C# are similar to function pointers in C/C++ and can be
used to pass methods as arguments to other methods, chained together, or even can
be used to call methods on events
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 a namespace?
Namespace in C# can be considered as a container in which you can define classes,
methods, interfaces, structures, or other child namespaces, such that classes with
the same name but different namespaces won’t cause any error. In C#, namespaces
are an efficient entity to organize codes for larger applications.
Encapsulation has some hidden advantages as well which make it the need of the
hour for every software developer such as preventing accidental corruption,
unauthorized access to entities, etc.
15. What are Access Modifiers in C# And Why are they important?
As the name suggests, access modifiers are keywords that define the scope of
entities in a C# code, as to which point an entity can be accessed from its point of
creation.
In particular, there are 4 access modifiers in C#: public, protected, internal, and
private.
Method hiding or Method shadowing is used to hide the implementation of the base
class method under the shadow of the child class method, with the help of a new
keyword.
This is done to clear the ambiguity when the child class has a method with the same
name as that of the base class and hence helps in abstraction.
Framework targeting helps guarantee that the application uses only functionality that
is available in the specified framework version.
Struct Class
Structs are value-type entities stored Class is a reference type entity
in stack memory. stored in heap memory.
A struct can neither have a virtual A class can have virtual methods
method nor an abstract method. as well as abstract methods.
BASIS FOR
DISPOSE( ) FINALIZE( )
COMPARISON
To access any element, all you need to do is just pass the value of the element
alongside enum’s variable, such as:
Interlocked functions only support int, long, double and float data types, as shown by
some of the examples below:
1) Add(ref int address1, int value): Interlocked function to add a value into an int
variable safely.
AutoResetEvent also calls the Reset() method by itself to unblock the thread under
consideration, whereas while using ManualResetEvent, the Reset() method has to be
called manually after the use of set() method.
Unlike other synchronization mechanisms in C#, such as Auto Reset Event which can
give the exclusive access of a resource to any thread that calls the set() method,
Mutex remembers the thread that gets the resource access, and only that same
thread can reset it, thereby giving the true mutual exclusion synchronization. If the
thread differs, a mutex exception is thrown.
30. What is the lock statement in C#?
If we want to prevent any class from being inherited, it can be done with the help of a
sealed keyword. So using a sealed keyword, a class denies the ability to create any
child entity of this class, thereby calling itself a sealed class. If by mistake, a child of
a sealed class is tried to be made, a compile-time error will occur.
● 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.
There are some third-party serialization formats as well that are supported in C#,
such as MessagePack (msgpack).
● If the occupied memory by the objects exceeds the pre-set threshold value.
● If the garbage collection method is called
● If your system has low physical memory
4. What are the types of classes in C#?
Class is an entity that encapsulates all the properties of its objects and instances as
a single unit. C# has four types of such classes:
● Static class: Static class, defined by the keyword ‘static’ does not allow
inheritance. Therefore, you cannot create an object for a static class.
● Partial class: Partial class, defined by the keyword ‘partial’ allows its members
to partially divide or share source (.cs) files.
● Abstract class: Abstract classes are classes that cannot be instantiated
where you cannot create objects. Abstract classes work on the OOPS concept
of abstraction. Abstraction helps to extract essential details and hide the
unessential ones.
● Sealed class: Sealed classes are classes that cannot be inherited. Use the
keyword sealed to restrict access to users to inherit that class.
Managed code runs on the managed runtime environment than the operating system
itself.
Benefits: Provides various services like a garbage collector, exception handling, etc.
Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that
works outside the .NET framework.
They don’t provide services of the high-level languages and therefore, run without
them. Such an example is C++.
6. What is the difference between an abstract class and an interface?
Let’s dig into the differences between an abstract class and an interface:
● Abstract classes are classes that cannot be instantiated ie. that cannot create
an object. The interface is like an abstract class because all the methods
inside the interface are abstract methods.
● Surprisingly, abstract classes can have both abstract and non-abstract
methods but all the methods of an interface are abstract methods.
● Since abstract classes can have both abstract and non-abstract methods, we
need to use the Abstract keyword to declare abstract methods. But in the
interface, there is no such need.
An abstract class has constructors while an interface encompasses none.
Wider use of development: Using C#, you are prone to create web applications or
gaming apps. C# has some fantastic features like automatic garbage collection,
interfaces, etc. which help build better applications.
Let’s look at 50 much asked and a comprehensive set of questions and answers on
C#.
Architecture of CLR
● If the occupied memory by the objects exceeds the pre-set threshold value.
● If the garbage collection method is called
● If your system has low physical memory
You can download a PDF version of C Sharp Interview Questions.
Download PDF
● Static class: Static class, defined by the keyword ‘static’ does not allow
inheritance. Therefore, you cannot create an object for a static class.
Sample code:
{
//static data members
//static methods
● Partial class: Partial class, defined by the keyword ‘partial’ allows its members
to partially divide or share source (.cs) files.
● Abstract class: Abstract classes are classes that cannot be instantiated
where you cannot create objects. Abstract classes work on the OOPS concept
of abstraction. Abstraction helps to extract essential details and hide the
unessential ones.
● Sealed class: Sealed classes are classes that cannot be inherited. Use the
keyword sealed to restrict access to users to inherit that class.
// data members
// methods
Managed code runs on the managed runtime environment than the operating system
itself.
Benefits: Provides various services like a garbage collector, exception handling, etc.
Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that
works outside the .NET framework.
They don’t provide services of the high-level languages and therefore, run without
them. Such an example is C++.
● Abstract classes are classes that cannot be instantiated ie. that cannot create
an object. The interface is like an abstract class because all the methods
inside the interface are abstract methods.
● Surprisingly, abstract classes can have both abstract and non-abstract
methods but all the methods of an interface are abstract methods.
● Since abstract classes can have both abstract and non-abstract methods, we
need to use the Abstract keyword to declare abstract methods. But in the
interface, there is no such need.
An abstract class has constructors while an interface encompasses none.
Ex.
Abstract class:
Interface:
void paint();
Extension methods help to add new methods to the existing ones. The methods that
are added are static. At times, when you want to add methods to an existing class
but don’t perceive the right to modify that class or don’t hold the rights, you can
create a new static class containing the new methods. Once the extended methods
are declared, bind this class with the existing one and see the methods will be added
to the existing one.
Multiple Inheritance in C#
Here, class C can inherit properties from Class A and Class B. Here is an example of
inheritance:
// C# program to illustrate
using System;
using System.Collections;
// Parent class 1
class Scaler {
// of features() method
// Creating ArrayList
ArrayList My_features= new ArrayList();
// My_features ArrayList
My_features.Add("Abstraction");
My_features.Add("Encapsulation");
My_features.Add("Inheritance");
Console.WriteLine(elements);
}
// Parent class 2
// of courses() method
// Creating ArrayList
// My_features ArrayList
My_features.Add("C++");
My_features.Add("C#");
My_features.Add("JScript");
Console.WriteLine("\nLanguages that use OOPS
concepts:");
Console.WriteLine(elements);
// Child class
obj.features();
obj.languages();
Instead, you can use interfaces to inherit the properties using the class name in the
signature.
Example:
Unboxing: Unboxing converts reference type (object) to value type (int, char, etc.)
using an explicit conversion process.
Example:
// code
You can easily split the functionalities of methods, interfaces, or structures into
multiple files. You can even add nested partial classes.
15. What is the difference between late binding and early binding in C#?
Late binding and early binding are examples of one of the primary concepts of OOPS:
Polymorphism.
For ex: one function calculateBill() will calculate bills of premium customers, basic
customers, and semi-premium customers based on their policies differently. The
calculation for all the customer objects is done differently using the same function
which is called polymorphism.
When an object is assigned to an object variable in C#, the .NET framework performs
the binding.
But when the binding happens at runtime, it is called late binding. Late binding
happens when the objects are dynamic (decided based on the data they hold) at run-
time. It is slower as it looks through during run-time.
For ex. An array of tea Atea[4]: [green tea, chamomile tea, black tea, lemon tea]. The
length of the array defines how many elements are present in the array.
In C#, the memory allocations for the elements of the array happen dynamically.
This is how values are stored in an array sequentially.
Arrays in C#
get{
set{
18. Difference between the Equality Operator (==) and Equals() Method
in C#?
Although both are used to compare two objects by value, still they both are used
differently.
For ex.:
int x = 10;
int y = 10;
Console.WriteLine( x == y);
Console.WriteLine(x.Equals(y));
Output:
True
True
Equality operator (==) is a reference type which means that if equality operator is
used, it will return true only if both the references point to the same object.
Equals() method: Equals method is used to compare the values carried by the
objects. int x=10, int y=10. If x==y is compared then, the values carried by x and y are
compared which is equal and therefore they return true.
19. What are the different ways in which a method can be Overloaded in
C#?
Overloading means when a method has the same name but carries different values
to use in a different context. Only the main() method cannot be overloaded.
double area = x * x;
return area;
double area = a * b;
return area;
Here, the method Area is used twice. In the first declaration, one argument is used
while in the second one, there were two arguments are used. Using different
parameters in the same method, we were able to overload the method area().
● Assembly
● Module
● Enum
● MethodInfo
● ConstructorInfo
● MemberInfo
● ParameterInfo
● Type
● FieldInfo
● EventInfo
● PropertyInfo
For ex:
class IB {
// Constant fields
// Main method
Output:
The value of xvar is 20.
On the other hand, with readonly keyword, you can assign the variable only when it is
declared or in a constructor of the same class in which it is declared.
Ex:
// Using constructor
xvar1 = b;
yvar2 = c;
Console.WriteLine("The value of xvar1 {0}, "+
// Main method
Output:
Constants are static by default while readonly should have a value assigned when
the constructor is declared.
Constants can be declared within functions while readonly modifiers can be used
with reference types.
StringBuilder simplifies the entire process of making changes to the existing string
object. Since the String class is immutable, it is costlier to create a new object every
time we need to make a change. So, the StringBuilder class comes into picture which
can be evoked using the System.Text namespace.
In case, a string object will not change throughout the entire program, then use String
class or else StringBuilder.
For ex:
string s = string.Empty;
Here, you’ll need to create 2001 objects out of which 2000 will be of no use.