Web Design that taps into the haromny and vision of your dreams.

C# Generics Variables

A look at C# generics and variables

Written By on in C#

352 words, estimated reading time 2 minutes.

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.

C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

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.

using System;
using System.Collections.Generic;
class Generics
    static void Main(string[] args)
        // create arrays of various types
        int[] intArray = { 1, 2, 3, 4, 5, 6 };
        double[] doubleArray = { 1.0, 2.0, 3.0, 4.0, 5.0 };
        char[] charArray = { 'A', 'B', 'C', 'D', 'E' };
    // generic method displays array of any type
    static void DisplayArray<E>(E[] array)
        Console.WriteLine("Display array of type " + array.GetType() + ":");
        foreach (E element in array)
            Console.Write(element + " ");

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.

using System;
using System.Collections.Generic;
class Generics
    static void Main(string[] args)
        MyGeneric<int> mygenericint = new MyGeneric<int>();
        mygenericint.GenericField = 13;
        MyGeneric<string> mygenericstring = new MyGeneric<string>();
        mygenericstring.GenericField = "xxx";
        // These lines will cause a compile error
        MyGeneric<int> mygenericint2 = new MyGeneric<int>();
        mygenericint2.GenericField = "xxx";
public class MyGeneric<T>
    public T GenericField;
    public void GenericMethod(T t)
        Console.WriteLine("GenericMethod parameter type: " + t.GetType());

Last updated on: Thursday 22nd June 2017


P.Rajesh Kumar

P.Rajesh Kumar

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

Reply to P.Rajesh Kumar


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me