top

Search

C# Tutorial

Generics in C# allow the creation and design of classes and methods that defer the specification of types until the class or method is declared. Simply put, Generics allow the creation of classes and methods that work with any data type.Salient Points of GenericsSome of the salient points about Generics are given as follows:Generics are mostly used to create collection classes.Generics can also be used to create generic classes, methods, interfaces, delegates, events etc.Code reuse, performance enhancement, type safety etc. are all benefits of Generics.There are many Generic collection classes in the Systems.Collections.Generic namespace that should be frequently used.Generic data type information can be obtained at run-time by using reflection.Generic Classes and Generic MethodsA Generic class can be defined using angle brackets <>. It has a type parameters that can handle any type that is specified. A Generic method is a method that is declared by using type parameters. Both Generic classes and methods are an integral part of Generics in C#.A program that demonstrates generic class and generic method in C# is given as follows:Source Code: Program to implement generic class and generic method in C#using System; public class GClass < T > { private T GMemberVar; public GClass(T data) {  GMemberVar = data; } public T GMethod(T GParam) {  Console.WriteLine("The Generic parameter is: {0}", GParam);  return GMemberVar; } public T genericProperty {  get;  set; } } public class Example { public static void Main() {  GClass < char > GObj1 = new GClass < char > ('A');  GClass < int > GObj2 = new GClass < int > (65);  char val1;  int val2;  Console.WriteLine("For a character variable");  val1 = GObj1.GMethod('Z');  Console.WriteLine("The Generic member variable is: {0}", val1);  Console.WriteLine("For an integer variable");  val2 = GObj2.GMethod(34);  Console.WriteLine("The Generic member variable is: {0}", val2); } }The output of the above program is as follows:For a character variable The Generic parameter is: Z The Generic member variable is: A For an integer variable The Generic parameter is: 34 The Generic member variable is: 65Now let us understand the above program.GClass is a generic class in the above program. It contains a generic member variable GMemberVar and a parameterized constructor that initializes the value of GMemberVar to data. The Generic method GMethod() displays the value of the parameter it recieves and returns GMemberVar. The code snippet for this is given as follows:public class GClass<T> {    private T GMemberVar;    public GClass(T data)    {        GMemberVar = data;    }    public T GMethod(T GParam )    {        Console.WriteLine("The Generic parameter is: {0}",  GParam);        return GMemberVar;    }    public T genericProperty { get; set; } }In the function Main(), there are two objects GObj1 and GObj2 that initialize their GMemberVar with A and 65 respectively. Then the method GMethod() is called using GObj1 and GObj2 respectively. The code snippet for this is given as follows:public static void Main() {        GClass<char> GObj1 = new GClass<char>('A');        GClass<int> GObj2 = new GClass<int>(65);        char val1;        int val2;        Console.WriteLine("For a charater variable");        val1 = GObj1.GMethod('Z');        Console.WriteLine("The Generic member variable is: {0}",  val1);        Console.WriteLine("For a integer variable");        val2 = GObj2.GMethod(34);        Console.WriteLine("The Generic member variable is: {0}",  val2); }
logo

C# Tutorial

Generics in C#

Generics in C# allow the creation and design of classes and methods that defer the specification of types until the class or method is declared. Simply put, Generics allow the creation of classes and methods that work with any data type.

Salient Points of Generics

Some of the salient points about Generics are given as follows:

  1. Generics are mostly used to create collection classes.
  2. Generics can also be used to create generic classes, methods, interfaces, delegates, events etc.
  3. Code reuse, performance enhancement, type safety etc. are all benefits of Generics.
  4. There are many Generic collection classes in the Systems.Collections.Generic namespace that should be frequently used.
  5. Generic data type information can be obtained at run-time by using reflection.

Generic Classes and Generic Methods

A Generic class can be defined using angle brackets <>. It has a type parameters that can handle any type that is specified. A Generic method is a method that is declared by using type parameters. Both Generic classes and methods are an integral part of Generics in C#.

A program that demonstrates generic class and generic method in C# is given as follows:

Source Code: Program to implement generic class and generic method in C#

using System;
public class GClass < T > {
private T GMemberVar;
public GClass(T data) {
 GMemberVar = data;
}
public T GMethod(T GParam) {
 Console.WriteLine("The Generic parameter is: {0}", GParam);
 return GMemberVar;
}
public T genericProperty {
 get;
 set;
}
}
public class Example {
public static void Main() {
 GClass < char > GObj1 = new GClass < char > ('A');
 GClass < int > GObj2 = new GClass < int > (65);
 char val1;
 int val2;
 Console.WriteLine("For a character variable");
 val1 = GObj1.GMethod('Z');
 Console.WriteLine("The Generic member variable is: {0}", val1);
 Console.WriteLine("For an integer variable");
 val2 = GObj2.GMethod(34);
 Console.WriteLine("The Generic member variable is: {0}", val2);
}
}

The output of the above program is as follows:

For a character variable
The Generic parameter is: Z
The Generic member variable is: A

For an integer variable
The Generic parameter is: 34
The Generic member variable is: 65

Now let us understand the above program.

GClass is a generic class in the above program. It contains a generic member variable GMemberVar and a parameterized constructor that initializes the value of GMemberVar to data. The Generic method GMethod() displays the value of the parameter it recieves and returns GMemberVar. The code snippet for this is given as follows:

public class GClass<T>
{
   private T GMemberVar;
   public GClass(T data)
   {
       GMemberVar = data;
   }
   public T GMethod(T GParam )
   {
       Console.WriteLine("The Generic parameter is: {0}",  GParam);
       return GMemberVar;
   }
   public T genericProperty { get; set; }
}

In the function Main(), there are two objects GObj1 and GObj2 that initialize their GMemberVar with A and 65 respectively. Then the method GMethod() is called using GObj1 and GObj2 respectively. The code snippet for this is given as follows:

public static void Main()
{
       GClass<char> GObj1 = new GClass<char>('A');
       GClass<int> GObj2 = new GClass<int>(65);
       char val1;
       int val2;
       Console.WriteLine("For a charater variable");
       val1 = GObj1.GMethod('Z');
       Console.WriteLine("The Generic member variable is: {0}",  val1);
       Console.WriteLine("For a integer variable");
       val2 = GObj2.GMethod(34);
       Console.WriteLine("The Generic member variable is: {0}",  val2);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial