Introduction
OOPS
1. What is OOPS and what is advantage of OOP?
2. What are OOP Concepts?
- Data Abstraction: Data Abstraction is a concept in which the internal and superfluous details of the implementation of a logic is hidden from an end user(who is using the program) .A user can use any of the data and method from the class without knowing about how this is created or what is the complexity behind it. In terms of a real world example, when we drive a bike and change the gears we don’t have to care about how internally its working, like how liver is pulled or how chain is set.
- Inheritance: Inheritance is most popular Concept in OOP’s .This provides a developer an advantage called reusability of code. Suppose a class is written having functions with specific logic, then we can derive that class into our newly created class and we don’t have to write the logic again for derived class functions, we can use them as it is.
- Data Encapsulation: Wrapping up of member data and member functions of a class in a single unit is called encapsulation. The visibility of the member functions,data members is set via access modifiers used in class.
- Polymorphism: Poly means many and morphism means many function The Concepts Introduces in the form of Many behaviours of an object.
- Message Communication: Message Communication means when an object passes the call to method of class for execution.

- Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
- Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
- Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
- Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
- Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
- Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
- Diving into OOP (Day 7): Properties in C# (A Practical Approach)
- Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
- Diving into OOP (Day 9): Understanding Events in C# (An Insight)
3. Polymorphism:
Method Overloading or Early Binding or Compile Time Polymorphism
- Let’s create a simple console application named
InheritanceAndPolymorphism
, and add a class namedOverload.cs and add three methods namedDisplayOverload
having varied parameters as follows,
Overload.cs
public class Overload { public void DisplayOverload(int a){ System.Console.WriteLine("DisplayOverload " + a); } public void DisplayOverload(string a){ System.Console.WriteLine("DisplayOverload " + a); } public void DisplayOverload(string a, int b){ System.Console.WriteLine("DisplayOverload " + a + b); } }
In the main method in Program.cs file, add the following code,
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.DisplayOverload(100); overload.DisplayOverload("method overloading"); overload.DisplayOverload("method overloading", 100); Console.ReadKey(); } }
Output
DisplayOverload method overloading
DisplayOverload method overloading100
Overload
contains three methods named DisplayOverload
, they only differ in the datatype of the parameters they consist of. In C# we can have methods with the same name, but the datatypes of their parameters should differ. This feature of C# is called method overloading. Therefore, we need not to remember lots of method names if a method differs in behavior, only providing different parameters to the methods can call a method individually.public void DisplayOverload() { } public int DisplayOverload(){ }
static void DisplayOverload(int a) { } public void DisplayOverload(int a) { } public void DisplayOverload(string a){ }
DisplayOverload
methods, that accept an int (integer). The only difference is that one method is marked static. Here the signature of the methods will be considered same as modifiers such as static are also not considered to be a part of method signature.private void DisplayOverload(int a) { }
private void DisplayOverload(out int a)
{
a = 100;
}
private void DisplayOverload(ref int a) { }
DisplayOverload
takes an int with different access modifiers i.e. out/ref etc, the signature on each is different.4. Role of Params Parameter in Polymorphism
- pass by value,
- Pass by reference,
- As an output parameter,
- Using parameter arrays.
public void DisplayOverload(int a, string a) { }
public void Display(int a)
{
string a;
}
Overload.cs
public class Overload { private string name = "Akhil";
public void Display()
{
Display2(ref name, ref name);
System.Console.WriteLine(name);
}
private void Display2(ref string x, ref string y)
{
System.Console.WriteLine(name);
x = “Akhil 1″;
System.Console.WriteLine(name);
y = “Akhil 2″;
System.Console.WriteLine(name);
name = “Akhil 3″;
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
Akhil
Akhil 1
Akhil 2
Akhil3
Display
the string name has a value of Akhil. Then by changing the string x to Akhil1, we are actually changing the string name to Akhil1 as name is passed by reference. Variables x and name refer to the same string in memory. Changing one changes the other. Again changing y also changes name variable as they refer to the same string anyways. Thus variables x, y and name refer to the same string in memory.Overload.cs
public class Overload { public void Display() { DisplayOverload(100, "Akhil", "Mittal", "OOP"); DisplayOverload(200, "Akhil"); DisplayOverload(300); }
private void DisplayOverload(int a, params string[] parameterArray)
{
foreach (string str in parameterArray)
Console.WriteLine(str + “ “ + a);
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
Mittal 100
OOP 100
Akhil 200
params
keyword.params
keyword can only be applied to the last argument of the method. So the n number of parameters can only be at the end.DisplayOverload
, the first argument has to be an integer, the rest can be from zero to an infinite number of strings.private void DisplayOverload(int a, params string[] parameterArray, int b) { }
params
keyword will be the last parameter in a method, this is already stated in the latest point to remember.Overload.cs
public class Overload { public void Display() { DisplayOverload(100, 200, 300); DisplayOverload(200, 100); DisplayOverload(200); }
private void DisplayOverload(int a, params int[] parameterArray)
{
foreach (var i in parameterArray)
Console.WriteLine(i + “ “ + a);
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
300 100
100 200
params
have the same data type.parameterArray
.private void DisplayOverload(int a, params string[][] parameterArray) { }
private void DisplayOverload(int a, params string[,] parameterArray) { }
params
argument must be a single dimensional array. Therefore [ ][ ]params
keyword with ref or out.Overload.cs
public class Overload { public void Display() { string[] names = {"Akhil", "Ekta", "Arsh"}; DisplayOverload(3, names); }
private void DisplayOverload(int a, params string[] parameterArray)
{
foreach (var s in parameterArray)
Console.WriteLine(s + “ “ + a);
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
Ekta 3
Arsh 3
names
is a string array which has been initialized using the short form. Internally when we call the function DisplayOverload
, C# converts the string array into individual strings.Overload.cs
public class Overload { public void Display() { string [] names = {"Akhil","Arsh"}; DisplayOverload(2, names, "Ekta"); }
private void DisplayOverload(int a, params string[] parameterArray)
{
foreach (var str in parameterArray)
Console.WriteLine(str + “ “ + a);
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output

DisplayOverload
, C# accumulates all the individual parameters and converts them into one big array for the params
statement.Overload.cs
public class Overload { public void Display() { int[] numbers = {10, 20, 30}; DisplayOverload(40, numbers); Console.WriteLine(numbers[1]); }
private void DisplayOverload(int a, params int[] parameterArray)
{
parameterArray[1] = 1000;
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
parameterArray[1]
of array has an initial value of 20 and in the method DisplayOverload
, we changed it to 1000. So the original value changes, this shows that the array is given to the method DisplayOverload
, Hence proved.Overload.cs
public class Overload { public void Display() { int number = 102; DisplayOverload(200, 1000, number, 200); Console.WriteLine(number); }
private void DisplayOverload(int a, params int[] parameterArray)
{
parameterArray[1] = 3000;
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
DisplayOverload
has no knowledge of number, so how can DisplayOverload
change the value of the int number? Therefore it remains the same.Overload.cs
public class Overload { public void Display() { DisplayOverload(200); DisplayOverload(200, 300); DisplayOverload(200, 300, 500, 600); }
private void DisplayOverload(int x, int y)
{
Console.WriteLine(“The two integers “ + x + “ “ + y);
}
private void DisplayOverload(params int[] parameterArray)
{
Console.WriteLine(“parameterArray”);
}
}
Program.cs
class Program { static void Main(string[] args) { Overload overload = new Overload(); overload.Display(); Console.ReadKey(); } }
Output
The two integers 200 300
parameterArray
params
statement and treats it as a stepchild. When we invoke DisplayOverload
only with one integer, C# can only call theDisplayOverload
that takes a params
as a parameter as it matches only one int. An array can contain one member too. The fun is with the DisplayOverload
that is called with two ints now. So here we have a dilemma. C# can call theparams DisplayOverload
or DisplayOverload
with the two ints. As discussed earlier, C# treats the params
as a second class member and therefore chooses the DisplayOverload
with two ints. When there are more than two ints like in the third method call, C# is void of choice but to grudgingly choose the DisplayOverload
with the params
. C# opts for the params
as a last resort before flagging an error.Overload.cs
public class Overload { public static void Display(params object[] objectParamArray) { foreach (object obj in objectParamArray) { Console.Write(obj.GetType().FullName + " "); } Console.WriteLine();
}
}
Program.cs
class Program { static void Main(string[] args) { object[] objArray = { 100, "Akhil", 200.300 }; object obj = objArray; Overload.Display(objArray); Overload.Display((object)objArray); Overload.Display(obj); Overload.Display((object[])obj); Console.ReadKey();
}
}
Output
System.Object[]
System.Object[]
System.Int32 System.String System.Double
Display
an array of object that looks like object. Since all the classes are derived from a common base class object, we can do that. The method Display gets an array of objectsobjectParamArray
. In the foreach
object class has a method named GetType
that returns an object that looks like Type, which too has a method named FullName
that returns the name of the type. Since three different types displayed. In the second method call of Display
we are casting objArray
to an object. Since there is no conversion available from converting an object to an object array i.e. object [ ], so only a one element object [ ] is created. It’s the same case in the third invocation and the last explicitly casts to an object array.Overload.cs
public class Overload { public static void Display(params object[] objectParamArray) { Console.WriteLine(objectParamArray.GetType().FullName); Console.WriteLine(objectParamArray.Length); Console.WriteLine(objectParamArray[0]);
}
}
Program.cs
class Program { static void Main(string[] args) { object[] objArray = { 100, "Akhil", 200.300 }; Overload.Display((object)objArray); Console.ReadKey(); } }
Output
1
System.Object[]
5. Conclusion

- C# recognizes the method by its parameters and not by its name.
- The return value/parameter type of a method is never the part of method signature if the names of the methods are same. So this is not polymorphism.
- Modifiers such as static are not considered as part of method signature.
- The signature of a method consists of its name, number and types of its formal parameters. The return type of a function is not part of the signature. Two methods can not have the same signature and also non-members cannot have the same name as members.
- Parameter names should be unique. And also we can not have a parameter name and a declared variable name in the same function as same.
- In case of pass by value, the value of the variable is passed and in the case of ref and out, the address of the reference is passed.
- This params keyword can only be applied to the last argument of the method.So the n number of parameters can only be at the end.
- C# is very smart to recognize if the penultimate argument and the params have the same data type.
- Parameter array must be a single dimensional array.

Read more:
- C# and ASP.NET Questions (All in one)
- MVC Interview Questions
- C# and ASP.NET Interview Questions and Answers
- Web Services and Windows Services Interview Questions
Other Series
My other series of articles:
For more informative articles visit my Blog.
15 comments