Popular Posts

There was an error in this gadget

Nov 16, 2012

Generics Classes - C#




Generic classes have type parameters. Several separate classes in a C# program, each with a different field type in them, can be replaced with a single generic class. The generic class introduces a type parameter. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.This becomes part of the class definition itself.

In this example, the letter T denotes a type that is only known based on the calling location. The program can act upon the instance of T like it is a real type, but it is not.



Create a console project then create a class  MyGenericClass.cs


public class MyGenericClass
    {
        //Generic variables 
        private T d1;
        private T d2;


        public MyGenericClass() { }

        //Generic constructor    
        public MyGenericClass(T a1, T a2)
        {
            d1 = a1;
            d2 = a2;
        }
        //Generic properties 
        public T D1
        {
            get { return d1; }
            set { d1 = value; }
        }

        public T D2
        {
            get { return d2; }
            set { d2 = value; }
        }

        public void ResetValues()
        {
            d1 = default(T);
            d2 = default(T);
           
        }

    }



In Program.cs file type the following CODE:

//contain int values 
MyGenericClass c1 = new MyGenericClass(5, 10); 
           // reset values to 0 
             c1.ResetValues();
          // Console.WriteLine();
            //contain  string values
MyGenericClass c2 = new MyGenericClass("a", "b");
            //reset values to null 
            c2.ResetValues();








For more visit http://msdn.microsoft.com/en-us/library/sz6zd40f.aspx

Generics- C#




- Generics are used to help make the code in the software components much more
reusable.
- They are a type of data structure that contains code that remains the same. 
- The data type of the parameters can change with each use.
- The usage within the data structure adapts to the different data type of the passed
variables. 


Create a console project then create a class Person.
public class Person
    {
       private string _Name;
       private int _Age;
      

      
      public string Name
       {
          get{return _Name;}
          set{_Name = value;}
       }

       public int Age
       {
          get{return _Age;}
          set{_Age = value;}
       }

    }



In Programs.cs file write the code:
System.Collections.Generic.List employees = new System.Collections.Generic.List();


// now add a couple of generic person objects to the generic collection 
             Person p1 = new Person();
             p1.Name = "John";
             p1.Age = 23;
             employees.Add(p1);

             Person p2 = new Person();
             p2.Name = "James";
             p2.Age = 34;
             employees.Add(p2);

            // now we have a type-safe collection of objects with the 
            // richness of Intellisense built in 
            // print out the contents of the collection 
            foreach (Person p in employees) 
            { 
               Console.WriteLine(String.Format("{0} - {1}", p.Name, p.Age)); 
            }

            Console.ReadKey();