Introduction
  1. At run-time May get information on the types used in a generic type .
  2. We can create generic classes by starting with pre define class.
  3. We may create our own generic classes.
  4. Generics started from framework 2.0.
  5. Generics allow we to define a class with a replacement holder for the type of its fields, methods, parameters, etc.
  6. Generics replace these replacement holder with some specific type at compile time.
  7. A generic class can be defined using angle brackets <>
Description
For example, the letter G denotes a type that is only known based on the calling location.The program can act upon the instance of G like it is a real type, but it is not(In in different sites G refer to T which denotes to Type That may be GT or other)
The Base Class library in c# for Generic is System.Collections.Generic;
Bellow is the Example,
In the Introduction I introduced the generic types may used in the method,property and parameters of a class .The bellow example is narates all these.
  1. class GenericTest<G>
  2. {
  3. G _value;
  4. public GenericTest(G t)
  5. {
  6. // equal type as the parameter.
  7. this._value = t;
  8. }
  9. public void Writevalue()
  10. {
  11. Console.WriteLine(this._value);
  12. }
  13. }
  14. }
  15. //In the Introduction I introduced the generic types may used in the method,property and parameters of a class .The bellow example is narates all these .
  16. class GenericClassDemo<G>
  17. {
  18. //Member variable of Generic type
  19. private G MemberVariable;
  20. //Here used as a parameter
  21. public GenericClassDemo(G value)
  22. {
  23. MemberVariable = value;
  24. }
  25. public G Method(G Parameter)
  26. {
  27. Console.WriteLine("Generic Parameter : " + typeof(G).ToString() + Parameter.ToString());
  28. Console.WriteLine("Return Generic type: " + typeof(G).ToString() + MemberVariable.ToString());
  29. return MemberVariable;
  30. }
  31. //Define a property with generic type
  32. public G Property { get; set; }
  33. }
  34. //The bellow example is narates the generic array
  35. public class GenericArray<G>
  36. {
  37. private G[] array;
  38. public GenericArray(int size)
  39. {
  40. array = new G[size + 1];
  41. }
  42. public G getItem(int index)
  43. {
  44. return array[index];
  45. }
  46. public void setItem(int index, G value)
  47. {
  48. array[index] = value;
  49. }
  50. }
The Generic Used Classes are Calling in main method those are define bellow,
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // int type parameter.
  6. GenericTest<int> gt1 = new GenericTest<int>(50);
  7. // Calling of the Writevalue method.
  8. gt1.Writevalue();
  9. // string type parameter.
  10. GenericTest<string> gt2 = new GenericTest<string>("c # corner");
  11. gt2.Writevalue();
  12. callingarryandaclass();
  13. Console.ReadLine();
  14. }
  15. static void callingarryandaclass()
  16. {
  17. GenericArray<double> doubleArray = new GenericArray<double>(5);
  18. //setting values
  19. for (int c = 0; c < 5; c++)
  20. {
  21. doubleArray.setItem(c, c * 5);
  22. }
  23. //retrieving the values
  24. for (int c = 0; c < 5; c++)
  25. {
  26. Console.Write(doubleArray.getItem(c) + " ");
  27. }
  28. Console.WriteLine();
  29. GenericClassDemo<int> GenericDemo = new GenericClassDemo<int>(10);
  30. int val = GenericDemo.Method(100);
  31. Console.WriteLine("value :" + val);
  32. }
  33. }
Generics can also applied to Interface,Abstract class,Static method,Event,Delegates and Operator.
Summery
Generic removes the possibilities of boxing and unboxing and Like Reflection At run-time May get information on the types.