Tuesday, 13 March 2018

043 C# Generics

Generic feature of. Net Framework.
It was added  in the version of 2.0. Generic allow  us delay declaration of the data type . User defined class , structure can be written and can be used as generic type during passing and initialization.

                             During compilation, compiler know how to handle the newly generated type. Generic is type safe  and  optimize code reuse.

Some points about generic
  • You can create class, structure, interface, method, delegates define by the user, considered a kind of  type.
  •  Generic comes under the namespace System.Collections.Generic
  •  Generic are type safe, no need to convert from object to actual type.
  •  Generic use the syntax , <T> is generic type. 
  • Genetic reduce overhead of explicit and implicit conversion, because during initialization , you are telling what type of data , will be hold in the collection. 

Here is the example of how a generic type is declared.

using System;
using System.Threading;
using System.Collections.Generic;

class Student<T1, T2>
{
     //Generic Type Variable Declaration
      private T1 mAge;

      //Generic Type Method
      public void GetFees(T1 value)
       {
        //State ment
      }

      // Return Generic Type
      public T2 genericMethod(T2 mAddress)
      {
         T2 obj;
         obj=mAddress;
        return obj;
       }

       //Generic Type Property
       public T1 StudentProperty
      {
          get
               {
               return mAge;
             }
         set
            {
              mAge = value; 
           }
     }
}

Initialize Constructor :
Generic type can be use to initialize a constructor. We can pass that Generic type to the constructor of a class,  depending upon the declaration. Here is a example.

using System;
using System.Threading;
using System.Collections.Generic;

class Student<T1, T2>
    {
          T1 Obj1;
          T2 Obj2;

               //Constructor Initialisation
                public Student(T1 a, T2 b)
                {
                  Obj1 = a;
                 Obj2 = b;
         }

              public void ShowData()
               {
                  Console.WriteLine("Obj1 :" + Obj1.ToString());
                  Console.WriteLine("Obj2 :" + Obj2.ToString());
              }
}

class School
{
         static void Main(string[] args)
        {
 
       //Example 1
           Student<string, string> myObj1;
           myObj1 = new Student<string, string>( "Hellow", " World");
           myObj1.ShowData();

         //Example 2
          Student<int, int> myObj2;
          myObj2 = new Student<int, int>(111,22);
          myObj2.ShowData();
 
    Console.ReadKey();
     }
}


Generic Method : A Method can also be Generic, we can pass generic type as a value type or reference type depending upon the declaration, the parameter can be single or multiple parameter. Generic support both.

using System;
using System.Threading;
using System.Collections.Generic;

class Student<T1, T2>
     {
         T1 Obj1;
        T2 Obj2;

          //Generic Type Methos
          public void MethodReturnGeneric<T1>(T1 a, T2 b)
         {
           T1 retu;
           retu = a;
           }

          //Generic Type Parameter , Pass By value
           public void Parameter_PassBy_Value(T1 a,T2 b)
           {
            Obj1 = a;
            Obj2 = b;
           Console.WriteLine("Obj1 :" + Obj1.ToString());
           Console.WriteLine("Obj2 :" + Obj2.ToString());
       }

          //Generic Type Parameter , Pass By Reference
         public void Parameter_PassBy_Reference(ref T1 a, ref T2 b)
         {
          Obj1 = a;
          Obj2 = b;
         Console.WriteLine("Obj1 :" + Obj1.ToString());
         Console.WriteLine("Obj2 :" + Obj2.ToString());
       }

}

class School
{
  static void Main(string[] args)
  {
     //Example 1
      Student<string, string> myObj1=new Student<string, string>();
     myObj1.Parameter_PassBy_Value("Hello", "World");
   //Example 2
     string str1 = "C#";
     string str2 = "Programming";
     myObj1.Parameter_PassBy_Reference(ref str1, ref str2);
    Console.ReadKey();
    }
}



Generic delegate : in the previous chapter we have seen, delegates are of three type
  • Single cast 
  • Multicast 
  • Generic. 
Generic type of delegate used Generic type , while raising event. Here is the example of generic type delegates.

class School
{
        public delegate T1 StudentCall(T1 a, T2 b);

        static void Main(string[] args)
        {
         
             StudentCall<int, int> gdInt = new StudentCall<int, int>(Example1);

         }

         public static int Example1(int a, int b)
         {
          return (a + b);
         }

        public static string Example2(string a, string b)
        {
         return (a + b);
        }

        public static int Example3(int a, int b)
        {
         return (a * b);
        }

       public static string Example3(string a, string b)
      {
        return (a + b);
       }

      public static string Example4(string a, int b)
      {
       return (b.ToString());
      }

}

Call The deligate:

School.StudentCall<int, int> obj = School.Example1;
Response.Write(School.Example1(5, 6));
//Output:11
Console.WriteLine(School.Example2("Hellow ", "World").ToString());
//Output:Hellow World




Generic interface : an interface can be declared using Generic type. Here is the example of interface using Generic type.

using System;
using System.Threading;
using System.Collections.Generic;
public interface IStudent1<T> : ICollection<T>
{
}
public interface IStudent2<T> : IEnumerable<T>
{
}
public interface IStudent3<T> : IList<T>
{
}
public interface IStudent4<T> : ICollection<T>
{
}

Example 1 

public interface Student<T> : IEnumerable<T>
{
         int Count { get; }
         bool Contains(T a);
         void CopyTo(T[] array, int ArrayIndex);
         bool IsReadOnly { get; }

        void Add(T a);
        void Clear();
        bool Remove(T a);
}

Example 2  
public interface Student<T> : IList<T>
{
           int Count { get; }
           bool Contains(T a);
           void CopyTo(T[] array, int ArrayIndex);
           bool IsReadOnly { get; }

           void Add(T a);
           void Clear();
           bool Remove(T a);
}

Example 3
   
public interface Student<T> : ICollection<T>
{
          int Count { get; }
          bool Contains(T a);
          void CopyTo(T[] array, int ArrayIndex);
          bool IsReadOnly { get; }

         void Add(T a);
         void Clear();
        bool Remove(T a);
}

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...