Compile Time Polymorphisum in C#
In this article we are going to see, polymorphism in C#.So, What is a polymorphism in C# ?As the polymorphism word itself describes its defination which means multiple forms.Poly means Multiple” or “Many” and Morphism means “changing” or “converting”.So in C# , polymorphism works in two scenario which are Run time and Compile time.First we will see Compile time polymorphism which is Method Overloading. Method overloading is an concept or approach where we can create Methods with same name but different signature. (Signature means number of passed argument/parameters considering with their data type and sequence) Following is a simple program to understand it.
Code Snippet:-
/// <summary>
/// class with Overloaded method with different signature but with same method name.
/// </summary>
public class CompileTimePolyMorph
{
/// <summary>
/// Method with integer as input parameter
/// </summary>
/// <param name="_input"></param>
public void WhatYouGot(int _input)
{
Console.WriteLine("I have " + _input + " choclate");
}
/// <summary>
/// Method with string as input parameter
/// </summary>
/// <param name="_input"></param>
public void WhatYouGot(string _input)
{
Console.WriteLine("I have "+ _input );
}
}
public class polymorphismDemo
{
public static void Main()
{
CompileTimePolyMorph obj = new CompileTimePolyMorph();
// Calling overloaded methods with there different input parameters.
obj.WhatYouGot(2);
obj.WhatYouGot("KitKat");
Console.ReadLine();
}
}
In above example , We have created Method “WhatYouGot” two times but with different input parameters. But still it would not give any error, why ? Because the C# compiler consider them two different methods due to their different input parameters.And this is the Method Overloading and it works at compile time that’s why we called it as compile time polymorphism.Point to remember is the Method Overloading is possible within a class.
Code Snippet:-
/// <summary>
/// class with Overloaded method with different signature but with same method name.
/// </summary>
public class CompileTimePolyMorph
{
/// <summary>
/// Method with integer as input parameter
/// </summary>
/// <param name="_input"></param>
public void WhatYouGot(int _input)
{
Console.WriteLine("I have " + _input + " choclate");
}
/// <summary>
/// Method with string as input parameter
/// </summary>
/// <param name="_input"></param>
public void WhatYouGot(string _input)
{
Console.WriteLine("I have "+ _input );
}
}
public class polymorphismDemo
{
public static void Main()
{
CompileTimePolyMorph obj = new CompileTimePolyMorph();
// Calling overloaded methods with there different input parameters.
obj.WhatYouGot(2);
obj.WhatYouGot("KitKat");
Console.ReadLine();
}
}
In above example , We have created Method “WhatYouGot” two times but with different input parameters. But still it would not give any error, why ? Because the C# compiler consider them two different methods due to their different input parameters.And this is the Method Overloading and it works at compile time that’s why we called it as compile time polymorphism.Point to remember is the Method Overloading is possible within a class.
Comments
Post a Comment