C# OOPS Interview Questions and Answers

Latest Basic and Advanced C# OOPS Interview Questions and Answers for Freshers and 2 - 4
years Experienced Dot Net developers and Testers with sample code on Access Modifiers, Static
Members, Reference Types, Overloading, Constructor, Operator and Function Overloading,
Encapsulation, Inheritance, Polymorphism, Overriding, Method Hiding, Abstract Class, Sealed
Classes, Interfaces and Constructor.
1) What is a class ?
A class is the generic definition of what an object is . A Class describes all the attributes of
object, as well as the methods that implements the behavior of member object. That means, class
is a template of an object. Easy way to understand a class is to look at an example . In the class
Employee given below, Name and Salary are the attributes of the class Person, Setter and Getter
methods are used to store and fetch data from the variable.
public class Employee
{
private String name;
private String Salary;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name; }
public String getSalary ()
{
return Salary;
}
public void setSalary (String Salary)
{
this. Salary = Salary;
}
}

2) What is an Object?
Object is an instance of a class, it contains real values instead of variables. For example, lets
create an instance of class Employee called “John”.
Employee John= new Employee();
Now we can access all the methods in the class “Employee” via object “John” as shown below.
John.setName(“XYZ”);

3) What are the Access Modifiers in C# ?
Different Access Modifier are - Public, Private, Protected, Internal, Protected Internal
 Public – When a method or attribute is defined as Public , It can be accessed from any
code in project. For example in the above Class “Employee” , getName(), setName() etc
are public.
 Private - When a method or attribute is defined as Private , It can be accessed by any
code within the containing type only. For example in the above Class “Employee” ,
attributes name and salary can be accessed with in the Class Employee Only. If an

public static double MinSalary { get { return minSalary. corresponding changes can be seen in emp1 object. static keyword is used. double var = Employee. To access a static variable. Here emp2 has an object instance of Employee Class . at first an object of the class should be created . Access is restricted to classes within the current project assembly Protected Internal – If an attribute or method is defined as Protected Internal . Methods and fields in a interface cannot be declared protected.   attribute or class are defined without access modifiers .ADO. Protected .Net Framework Interview Questions and Answers 4) Define Static Members in C# ? If an attributes value had to be same across all the instances of the same class . Then using that instance of class . For example if the Minimum salary should be set for all employees in the employee class. attributes or methods can be accessed.When an attribute and methods are defined as protected. it can be accessed by any method in inherited classes and any method within the same class. use the following code private static double MinSalary = 30000. private double minimumSalary. 6) Define Property in C# ? Properties are a type of class member . } set { minSalary = value. But emp1 object is set as emp2. Access is restricted to classes within the current project assembly and types derived from the containing class.. we will Create a property. Employee emp2 = new Employee(). For the code given below.Net Interview Questions and Answers 5) Define Reference Types in C# ? Let us explain this with an example . emp1 = emp2. that are exposed to outside world as a pair of Methods. The protected access modifier cannot be applied to class and interfaces. Must Read . Employee emp1. } } . we don't want to create an instance of the class having the static variable.For example for the static field Minsalary. We can directly refer that static variable as shown below. Must Read . Internal – If an attribute or method is defined as Internal . its default access modifier will be private. What this means is that object emp2 is refereed in emp1 and not that emp2 is copied into emp1.MinSalary . When a change is made in emp2 object. To access a private or public attributes or methods in a class.

Message). it takes one variable. MinSalary.Database Interview Questions and Answers 7) Define Overloading in C# ? When methods are created with same name . For example public class Employee { public void Employee() {} public void Employee(String Name) {} } . Don't Miss . “WriteLine” method takes two variable.net ? In Constructor overloading. get Method will get triggered and value in minimumSalary field will be returned. n number of functions can be created for same class. For example . In first instance .WriteLine("The message is {0}".WriteLine(x). Different type of overloading in C# are 1) Constructor overloading 2) Function overloading 3) Operator overloading Must Read . WriteLine method in console class is an example for overloading. In the second instance . Console. Console. set Method will get triggered and value will be stored in minimumSalary field .When we execute .net ? In Function overloading. Employee. n number of constructors can be created for same class. but with different signature its called overloading. For example public class Employee { public Employee() {} public Employee(String Name) {} } 9) Define Function Overloading in C# . But the signatures of each constructor should vary.SQL Query Interview Questions and Answers 8) Define Constructor Overloading in C# . MinSalary = 3000. But the signatures of each function should vary.So when we execute the following lines code double minSal = Employee.

Height . Height=h. } } . public static void Main() { Rectangle obj1 =new Rectangle().For operator Overloading . When first if condition is triggered. first overloaded function in the rectangle class will be triggered. } if(obj1 < obj2) { Console. } } Let us call the operator overloaded functions from the method below. public Rectangle(int w. When second if condition is triggered. Rectangle obj2 =new Rectangle(). } public static bool operator <(Rectangle a.WriteLine("Rectangle1 is greater than Rectangle2").net ? We had seen function overloading in the previous example. second overloaded function in the rectangle class will be triggered. class Rectangle { private int Height. private int Width. if(obj1 > obj2) { Console.Height > b. We define a class rectangle with two operator overloading methods. we will have look at the example below.Height .Rectangle b) { return a.WriteLine("Rectangle1 is less than Rectangle2").int h) { Width=w. } public static bool operator >(Rectangle a.10) Define Operator Overloading in C# .Rectangle b) { return a.Height < b.

So if a user set a negative value for MinSalary . let us have a look at 2 classes shown below. So if we give a third party code to have complete control over the field without any validation it can adversely affect the functionality. In the above example .base class and derived classes .WriteLine("Right Hand Drive").WriteLine("Derived Class Ford"). } public void DriveType() { Console. inheritance is a way to reuse code of existing objects. private field “minimumSalary” is an important part of our classes. This is in line with OOPS Concept that an external user should know about the what an object does. } } class Ford : Car { public Ford() { Console. } } . Idea behind this is that . we can put a validation in set method to avoid negative values as shown below set { if(value > 0) { minSalary = value. How it does it should be decided by the program. } public void Price() { Console. class Car { public Car() { Console.11) Define Data Encapsulation in c# ? Data Encapsulation is defined as the process of hiding the important fields from the end user.WriteLine("Base Class Car"). A class can inherit attributes and methods from existing class called base class or parent class. } } 12) Define Inheritance in C# ? In object-oriented programming (OOP). we had used getters and setters to set value for MinSalary. In inheritance there will be two classes . For more clarity on this topic . The class which inherits from base class is called derived classes or child class. Here Class Car is Base Class and Class Ford is derived class.WriteLine("Ford Price : 100K $").

WriteLine("Ford Price : 100K $"). Best example is Overriding 15) Define Virtual Keyword in C# ? When we want to give permission to derived class to override a method in base class. If you want inherit multiple base classes.WriteLine("Base Class Car"). we are able to invoke the method because of inheriting Base Class methods to derived class. CarFord.WriteLine("Right Hand Drive"). . CarFord. } } class Ford : Car { public Ford() { Console. Best example is Overloading 2) Runtime polymorphism. Even though there is no method called DriveType() in Class Ford. class Car { public Car() { Console. all the methods and attributes of Base Class car is available in Derived Class Ford. } public void Price() { Console. Ford CarFord = new Ford().Price().DriveType(). constructors of the Base and Derived class get invoked. For example lets us look at the classes Car and Ford as shown below. 13) Define Multiple Inheritance in C# ? In C# . use interface. } public virtual void DriveType() { Console. 14) What is Polymorphism in C# ? Ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. When an object of class Ford is created .WriteLine("Derived Class Ford"). There are two types of polymorphism 1) Compile time polymorphism. Base Class Car Derived Class Ford Right Hand Drive Ford Price : 100K $ What this means is that .When we execute following lines of code . derived classes can inherit from only one base class. Output Generated is given below. Virtual keyword is used .

} } class Ford : Car { public void DriveType() { Console. Base Class Car Right Hand Drive Base Class Car Derived Class Ford Right Hand 16) Define overriding in c# ? To override a base class method which is defined as virtual .WriteLine("Right Hand ").DriveType(). Output is as given below. CarFord = new Ford().WriteLine("Right Hand ").DriveType(). Override keyword is used. In the above example . class Car { public void DriveType() { Console. } } . CarFord. This is called Method Hiding. derived class can implement the same method in derived class with same signature.WriteLine("Right Hand Drive"). 17) Define Method Hiding in C# ? If the derived class doesn't want to use methods in base class . For example in the classes given below.} public override void DriveType() { Console. method DriveType is overrided in derived class. DriveType() is implemented in the derived class with same signature. CarFord. } } When following lines of code get executed Car CarFord = new Car().

in the classes given below . . abstract class Car { public Car() { Console. } } Method DriveType get implemented in derived class. method DriveType is defined as abstract. For example .18) Define Abstract Class in C#? If we don't want a class object to be created define the class as abstract. If a method in abstract id defined as abstract . } public abstract void DriveType(). public sealed class Car { public Car() { Console.WriteLine("Right Hand "). There wont be any implementation of the methods in Interface.WriteLine("Base Class Car"). 19) Define Sealed Classes in c# ? If a class is defined as Sealed it cannot be inherited in derived class. } class Ford : Car { public void DriveType() { Console. Example is given below. } public void DriveType() { Console.WriteLine("Right Hand "). } } 20) Define Interfaces in C# ? An interface is similar to a class with method signatures. it must be implemented in derived class. An abstract class can have abstract and non abstract classes.WriteLine("Base Class Car"). Classes which implements interface should have implementation of methods defined in abstract class.

interface Breaks { void BreakType().WriteLine("Power Break"). } interface Wheels { void WheelType().WriteLine("Derived Class Ford").WriteLine("Bridgestone"). } public void BreakType() { Console. } public void WheelType() { Console. } } 22) Define Destructor in C# ? Destructor is a special method that get invoked / called automatically whenever an object of a given class gets destroyed.WriteLine("Ford Price : 100K $"). . } public void Price() { Console.21) Define Constructor in C# ? Constructor is a special method that get invoked / called automatically whenever an object of a given class gets instantiated. } When ever an instance of class car is created from the same class or its derived class(Except Few Scenarios) Constructor get called and sequence of code written in the constructor get executed.WriteLine("Base Class Car"). } class Ford : Breaks. Main idea behind using destructor is to free the memory used by the object. Wheels { public Ford() { Console. For example in our class car constructor is defined as shown below public Car() { Console.

// variable need to be initialized i = 3. If you observe above code first we declared variable and initialized with value 3 before it pass a ref parameter to Refsample method Example class Program { static void Main() { int i. Declaration of Ref Parameter Generally we will use ref parameters like as shown below int i=3. Ref keyword will pass parameter as a reference this means when the value of parameter is changed in called method it get reflected in calling method also.WriteLine(i). } public static void Refsample(ref int val1) { val1 += 10. Console. // variable need to be initialized Refsample(ref i).Ref Parameter If you want to pass a variable as ref parameter you need to initialize it before you pass it as ref parameter to method. Refsample(ref i). } } When we run above code we will get like as shown below Output *********Output************ 13 *********Output************ As we discussed if ref parameter value changed in called method that parameter value reflected in calling method also .

WriteLine(j). Out keyword also will pass parameter as a reference but here out parameter must be initialized in called method before it return value to calling method. val2 = 10. Console. Declaration of Out Parameter Generally we will use out parameters like as shown below int i. Console.j. } public static int Outsample(out int val1.WriteLine(i).j. out j). out int val2) { val1 = 5. } } If we observe code we implemented as per our discussion like out parameter values must be initialized in called method before it return values to calling method When we run above code we will get like as shown below Output *********Output************ 5 10 *********Output************ . return 0. // No need to initialize variable Outsample(out i. // No need to initialize variable Outsample(out i.Out Parameter If you want to pass a variable as out parameter you don’t need to initialize it before you pass it as out parameter to method. out j). If you observe above code first we declared variable and we it pass a out parameter to Outsample method without initialize the values to variables Example class Program { static void Main() { int i.