C# Interview Questions
C# Interview Questions
In this article I will cover all the possible interview questions of the C# programming language and explain
in as simple language as possible so it becomes understandable by every fellow programmer.
C# is an enormous programming language with many concepts which is being constantly updated by
Microsoft and hence the Questions and Answer list cannot be constant.
Thus this article will be periodically updated with the new questions that fit best into this list of C#
Interview Questions and Answers.
1. What is C#?
C#
C++
C++ is a low level programming language built by adding object oriented concepts to the C
programming language
It is NOT similar to Java.
C++ itself is a language and does not inherit any features of other languages.
Struct
Reference Type i.e. whenever a variable is created, it holds the reference (address) while the
value is stored some location.
Copying a Class variable, copies the reference while the value remains on same location.
A new variable is created in Heap memory.
Can be inherited.
Can be of Abstract type.
Can contain Private parameterless constructors.
Supports Destructor.
String
Example:
string s = "Mudassar";
s = string.Concat(s, "Khan");
StringBuilder
Example:
Example:
public class A
{
public static void C()
{
//Invalid
this.D();
}
public void D()
{
//Valid
C();
}
}
A Static class is a class that cannot be instantiated i.e. its object cannot be created.
Static class
Example:
//Valid
F();
}
}
Non-Static class
Example:
public class A
{
public object C;
public static object G;
//Valid
var q = G;
//Invalid
E();
}
public void E()
{
//Valid.
var p = this.C;
//Valid
var q = G;
//Valid
D();
}
}
A Namespace is used to organize classes. Example of in-built .Net Namespace is the System
Namespace.
One can create his own Namespace and define the scope of the classes belonging to the
Namespace.
Keyword using is used in order to access a class belonging to a particular Namespace.
A class belonging to a Namespace can be accessed only when the Namespace is made
accessible with the help of using directive.
A namespace does not have any access modifiers i.e. namespace cannot be public, private, etc.
A namespace can belong to multiple assemblies and also a single assembly can have multiple
namespaces.
Example:
namespace MySpace
{
public class A
{
public static void Fun()
{
}
}
}
namespace YourSpace
{
//Accesing the Namespace
using MySpace;
public class B
{
public void Fun()
{
//Accessing the class of external namespace
A.Fun();
}
}
}
A Sealed class is a class which cannot be inherited. The sealed keyword is used to prohibit inheritance of
a particular class in C#.
Example:
An Internal class is a class which cannot be used outside its Assembly. The internal keyword is used to
mark a particular class Internal i.e. it restrict its access outside the Assembly.
Example:
internal class A
{
public void Fun()
{
}
}
//Valid
public class B : A
{
public static void Fun()
{
}
}
public class C
{
public static void Fun()
{
//Valid
A a = new A();
a.Fun();
}
}
An Abstract class is a special class which is majorly used for inheritance and it cannot be instantiated.
Cannot be instantiated i.e. object cannot be created using the new keyword.
Can contain both Abstract and Non-Abstract members.
Abstract members are simply declared and are defined in the classes deriving the Abstract class.
Abstract members are defined by using the override keyword.
Non-Abstract members are defined within the Abstract class.
Non-Abstract members can be accessed within the derived classes only if marked public or
protected.
Private Non-Abstract members are not accessible outside the Abstract class.
Abstract and Non-Abstract members can be accessed using the derived classes.
Does not support Multiple Inheritance.
Example:
//Valid
B b = new B();
b.Fun1();
b.Fun2();
b.Fun4();
}
}
Cannot be instantiated i.e. object cannot be created using the new keyword.
Can contain only declaration of Members and not definition.
Members are simply declared and are defined in the classes deriving the Interface.
Members are defined without using the override keyword.
Cannot contain Access modifiers i.e. Members cannot be public, private, etc.
The derived member can only be public.
Does support Multiple Inheritance.
Example:
public interface A
{
void Fun1();
void Fun2();
}
public class B : A
{
public void Fun1()
{
}
//Error: 'B' does not implement interface member 'A.Fun2()'
//'B.Fun2()' cannot implement an interface member because it is not public
private void Fun2()
{
}
}
public class C
{
public static void Fun3()
{
//Compiler Error: Cannot create an instance of the abstract class or
interface 'A'
A a = new A();
//Valid
B b = new B();
b.Fun1();
//Invalid
b.Fun2();
}
}