Website development and design blog, tutorials and inspiration

C# Generics Variables

A look at C# generics and variables

By , 23rd July 2008 in C#

C# Generics are a new feature in the .Net Framework 2.0+ which makes it possible to design classes and methods that do not specify data types until the class or method is declared and instantiated by client code.
 

By using a generic type parameter you can write a single class that other client code can use without incurring the cost or risk of casts or boxing operations.

In this first example, we can see how a method can accept an array of various types and output the values. Without generics you would need to override the method for each data type, causing problems for scalability and custom types.

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Generics
  5. {
  6. static void Main(string[] args)
  7. {
  8. // create arrays of various types
  9. int[] intArray = { 1, 2, 3, 4, 5, 6 };
  10. double[] doubleArray = { 1.0, 2.0, 3.0, 4.0, 5.0 };
  11. char[] charArray = { 'A', 'B', 'C', 'D', 'E' };
  12.  
  13. DisplayArray(intArray);
  14. DisplayArray(doubleArray);
  15. DisplayArray(charArray);
  16.  
  17. Console.ReadLine();
  18. }
  19.  
  20. // generic method displays array of any type
  21. static void DisplayArray<E>(E[] array)
  22. {
  23. Console.WriteLine("Display array of type " + array.GetType() + ":");
  24. foreach (E element in array)
  25. Console.Write(element + " ");
  26. }
  27. }

You can see how the one method will handle requests for an array of ints, doubles and chars. We can also use reflection and the GetType method to determine the type of array passed as the generic parameter.

In the next example, we can see a generic class with a generic method and a generic field. We can create an instance of the class as an int or a string and populate the field and call the method with parameters of the same type as the generic class, but if you create an instance of type int, you cannot assign strings to it.

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Generics
  5. {
  6. static void Main(string[] args)
  7. {
  8. MyGeneric<int> mygenericint = new MyGeneric<int>();
  9. mygenericint.GenericField = 13;
  10. mygenericint.GenericMethod(42);
  11.  
  12. MyGeneric<string> mygenericstring = new MyGeneric<string>();
  13. mygenericstring.GenericField = "xxx";
  14. mygenericstring.GenericMethod("xxx");
  15.  
  16. // These lines will cause a compile error
  17. MyGeneric<int> mygenericint2 = new MyGeneric<int>();
  18. mygenericint2.GenericField = "xxx";
  19. mygenericint2.GenericMethod("xxx");
  20. }
  21. }
  22.  
  23. public class MyGeneric<T>
  24. {
  25. public T GenericField;
  26. public void GenericMethod(T t)
  27. {
  28. Console.WriteLine("GenericMethod parameter type: " + t.GetType());
  29. }
  30. }
Comments
  1. P.Rajesh Kumar
    P.Rajesh Kumar

    These are nice series which makes a beginner feel like a mid level developer

Leave a Reply

Your email address will not be published.