Skip to main content

Generic method overloading

Introduction

I would like to introduce generic method overloading.

It would be good for you to already understand generics in C# but if not then please visit the following link.

Basics of Generic Classes in C#

First we will understand simple overloading then move towards generic overloading.

Steps and code

Add the following class to the project:
1.  public class SimpleClass  
2.  {  
3.      public void GetData(int x)  
4.      {  
5.          Console.WriteLine("INSIDE GetData and Datetype:" + x.GetType().Name);  
6.      }  
7.    
8.      public void GetData(string xStr)  
9.      {  
10.         Console.WriteLine("INSIDE GetData and Datetype:" + xStr.GetType().Name);  
11.     }  
12.  

Program.cs
1.  class Program  
2.  {  
3.      static void Main(string[] args)  
4.      {  
5.          SimpleClass o = new SimpleClass();  
6.          o.GetData(345);  
7.          o.GetData("Devesh is testing the code");  
8.          Console.ReadKey();  
9.      }  
10.  

Running the Code

The following will be the output:





Understanding the code

We have created the GetData function with overloaded functions having integer and string parameters.

When we passed an integer to the method first GetData (int x ) is called else if we passed a string then GetData(string xStr) is called.


Now we have a basic knowledge of method overloading.

Let us move towards
 Generic Method Overloading.

Now we are updating the GetData() method as a generic method.
1.  public void GetData<T>(T obj)  
2.  {  
3.      Console.WriteLine("INSIDE GetData<T>,"+ obj.GetType().Name);  
4.   

The following would be the complete code:
1.  public class SimpleDemoClass  
2.  {            

3.       public void GetData<T>(T obj)  
4.       {  
5.          Console.WriteLine("INSIDE GetData<T>,"+ obj.GetType().Name);  
6.       }        

7.       public void GetData(int x)  
8.       {  
9.          Console.WriteLine("INSIDE GetData" + x.GetType().Name);  
10.      }        

11.      public void GetxNextData<T>(T obj)  
12.      {  
13.          GetData(obj);  
14.      }      

15.  }  

Program.cs
1.  class Program  
2.  {  
3.        Static void Main(string[] args)  
4.        {    

5.             SimpleDemoClass sobj = new SimpleDemoClass();  
6.             sobj.GetData("data is for testing by-Devesh");  
7.             sobj.GetData(95);  
8.             sobj.GetxNextData(1234);  
9.             Console.ReadKey();  
10.        }  
11.  

Running code






Understanding the code.
  • When we called sobj.GetData("data is for testing by-Devesh");
  • Getdata<T>(T obj) is called. Because we are passing a string and this is implicitly an object
  • When we called sobj.GetData(95);, GetData(int x ) is called because we are passing an integer to the GetData method.
  • During runtime, the compiler decides to invoke the best sutiable method to be invoked
Conclusion

Here we learned the basics of generic method overloading.


Comments

Post a Comment

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Object Oriented Programming in JavaScript

Introduction JavaScript is a prototype-based programming style of object-oriented programming in which classes are not present. It can support OOP because it supports inheritance through prototyping as well as properties and methods. Object Oriented Programming in JavaScript is known as prototype-based programming. Creating Class in JavaScript  the following syntax is used for declaring a class in JavaScript: function Emp() {       alert( 'Emp instantiated' );   }   Here Emp can act as a class in JavaScript.  The body of Emp acts as a constructor and is called as soon as we create an object of the class.   Creating Objects of Emp Class Using the following syntax we can create an object of the Emp class: var Emp1 =  new  Emp();   As soon as we create an object of the Emp class the constructor will be called. Here the above function Emp would be treated as ...