Hi All, I researched on .NET and collected important interview questions on C#.NET Object Oriented Programming.
I am posting interview questions with answers parts by parts.
Please Find Other Interview Questions
C#.NET - Object Oriented Programming interview questions with answers - Part 2
C#.NET - Object Oriented Programming interview questions with answers - Part 1
Note:
To initialize a static class or a static fields you must create a static constructor.
Note:
It is better to make data members as private because if you make them as public anyone can access it from outside. So, protect data members by making them as private and initialize them through the properties and make properties as public.
What is a default constructor?
A constructor which is not have any arguments.
Note:
If your class doesn't have any default constructor. a default constructor will automatically generates and assign default values to the data members.
Note:
All C# types including premitive types such as int and double, inherit from a sigle base Object type.
Note:
Assemblies contains the executable code in the form of IL code, and symbolic information in the form of metadata.
What is the default access level of constructor?
private
Note:
Generally private constructors used in the classes that contains the static members only. Like Math class. Match class have static members and methods.
Note:
We can use a private constructor to initialize the static members of the class.
Can you inherit the class which is have the private constructor?
No, you cannot.
In which type of situations we use private constructors?
If you want to prevent the user from instantiating the class we can use private constructor.
In which situtation you will use static constructor?
Generally in the situation when you want to initialize the static data of the class.
Explain about destructor?
Destructor is used to released the instances of the class.
Structs doesnt support the destructors.
A class should have only one destructor.
Destructor will call automatically by the garbage collector.
Destructor cannot be overloaded and inherited.
Destructor doesn't have modifiers as well as parameters.
Destructor internally call the Finalize() method.
What Object initializers do?
Object initializers allows us to access the fields and properties of the class at the time of the object creation. Instead of declaring the parametarised constructor in the class to assign the values to the fields and properties we can use the Object initializers.
Note:
While you initializing data to the members of the class using object initilizers compiler will call the default constructor of the class.
So, If a class have the private default constructor object initializers will give the compile time error.
What is a Nested Type?
A type wich is declared under the class or the struct.
By default nested types are private.
You can get all the parent type members inside the nested type.
You can create the object of the nested type as follows.
Parent.Nested obj=new Parent.Nested();
Note:
Partial method declaration must starts with the contextual keyword and must return the void.
Partial methods cannot be virtual.
Partial methods can be static.
Explain Anonymous Types?
Anonymous types are introduced in C# 3.0.
These are the datatypes generated at runtime by the compiler.
These are helpful in LINQ queries.
EX:
var employees=from employee in Employees
select new {employee.Name, employee.Salary}
Instead of selecting the whole properties of the employee we can select few of them.
Note:
Value types donnot allow inheritance
Note:
?? operator used to compare with nullable types.
Note:
Static methods cannot be inherited or oveerriden.
Explain about partial method?
Partials methods are defined their signature in once partial type and implemented in another partial type.
If partial method has no implementation the compiler will remove the partial method from the class at compile time.
Paritial methods are always private.
Partial methods must return void.
In both signature and implemtation signature should match.
Assume two interfaces have one method with same name and return type.
interface IInterface1
{
void Method1();
}
interface IInterface2
{
void Method1();
}
you are inheriting those two interfaces into one class.
class Program:IInterface1,IInterface2
{
}
How can you implement those methods in your derived class and how can you call the respected methods of interfaces?
interface IInterface1
{
void Method1();
}
interface IInterface2
{
void Method1();
}
class Program:IInterface1,IInterface2
{
static void Main()
{
IInterface1 aa = new Program();
aa.Method1();
IInterface2 aa2 = new Program();
aa2.Method1();
}
void IInterface1.Method1()
{
Console.WriteLine("Methods of IInterface1");
}
void IInterface2.Method1()
{
Console.WriteLine("Methods of IInterface2");
}
}
// Output
// Methods of IInterface1
// Methods of IInterface2
What is encapsulation?
Wrapping up of a data into a single unit (i.e. class) is called encapsulation.
It is a technique to hide the internal members of a class.
We can hide the private information and we can show the public information using encapsulation.
EX: hiding the properties and exposing the property to access the private data.