Understand Generic class and Generic function in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 3851 red flag

Concept of Generic class and Generic function in c#

Understand Generic class and Generic function in C#

Here we will understand concept of Generic class and Generic function in C#.  The concept of generic is very important when we want to implement same concept in different situation. I hope you may aware of generic collection in c# where we can store any data type in single collection object. Here we will start with generic class.

Generic class

We can implement Generic class when we don’t know exact object type of class. In the example below, we have implemented one simple generic class called MyGeneric. If you closely observe definition of MyGeneric class ,you will find MyGeneric class is taking argument in run time and depending on argument it’s creating object within constructor.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class MyGeneric<T>
    {
        T obj;
         public MyGeneric(T Ob)
         {
             obj = Ob;
         }
         public T GetObject()
         {
             return obj;
         }
         public void DisplayType()
         {
             Console.WriteLine("Current object type is" +obj.GetType());
         }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyGeneric<int> A = new MyGeneric<int>(100);
            Console.WriteLine("Type of A:- " +  A.GetType());
            int a = A.GetObject();
            Console.WriteLine("Value of A:- " +  a);
 
            Console.ReadLine();
        }
    }
}

 


If we call DisplayType() function then we will get actual object type of MyGeneric class. In example we have passed one integer as argument of generic class.

Generic method

Generic method comes in picture when we don’t know argument type exactly. For example, there might be situation where one function will take different types of parameter and the type will define in run time. In this situation we have to implement Generic Method which can able to accept different type parameter. Here we have implemented simple example.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class MyGeneric
    {
        public void GenericMethod<T>(T obj)
        {
        Console.WriteLine("Argument type :" + obj.GetType() + " Argument Value : "+  obj.ToString());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyGeneric g = new MyGeneric();
            g.GenericMethod(100);
  g.GenericMethod("Sourav");
            Console.ReadLine();
        }
    }
}

Here is sample output.


Here GenericMethod is the example of generic method and from Main() function first we are sending integer as argument and in second time we are sending String as argument.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)