Click here to Skip to main content
12,754,090 members (29,210 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

64.6K views
17 bookmarked
Posted 28 Feb 2014

C# Generics

, 28 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
About C# generics

First of all, let's take an example where generics can be of great help.

Suppose you are writing a class for Stack with methods Push() and Pop(). You have to implement this class in such a way that it works for any data type. Now you will have two options if generic was not there to help you.

  • Define classes for each data types. You can achieve the solution using this approach. However there are many disadvantages of this approach:
    1. You are writing redundant code.
    2. If you need to support a new data type, then you need to add a new class for that.
    3. You are discouraging code reusability.
  • Defined class for data type as object. This is somewhat better than the first approach. However, it has its own disadvantages.
    1. Performance would be degraded because of boxing and un-boxing.
    2. TypeSafety is lost. Your solution will not be type safe.

Generic solves all the above problems. It:

  • allows you to define type-safe data structures, without providing actual data types.
  • allows you to write a class or method that can work with any data type.

Generic Constraints

As you know, you can use any data type with generic classes. Like you can use int, float, double data types with generic Stack class. However, sometimes we need to restrict the data types which can be used with particular generic class or methods. We can achieve this using generic constraints.

Generic Constraints are Categorized as Below

Derivation constraints – Restrict the generic type parameter to be derivative of specified interface or class.

public class LinkedList<K,T> where K : IComparable
// K data type must implement IComparable
public class LinkedList<K,T> where K : IComparable<K>
// K data type must implement IComparable<T>
public class LinkedList<K,T> : IEnumerable<T> where K : IComparable<K>
// Derive from IEnumerable<T> and K data type must implement IComparable<K>
public class LinkedList<K,T> where K : IComparable<K>,IConvertible
// K data type must implement IComparable<K> and IConvertible
public class LinkedList where K : IComparable,IConvertible 
// K data type must implement both IComparable and IConvertible

Constructor Constraints – Restricts the generic type parameter to define default constructor.

Suppose you have a generic class Node and this class has a property of data type N. While instantiating object of data type Node, you want to instantiate the object of data type N inside Node class. In this case, the compiler will try to invoke default constructor of the class N. However, if the default constructor is not defined for class N, it will throw exception while instantiating object of N.

You can restrict the generic data type to have default constructor.

class Node<K,T> where T : new() // T must have default constructor
{
   public K Key;
   public T Item;
   public Node<K,T> NextNode;
   public Node()
   {
      Key      = default(K);
      Item     = new T();
      NextNode = null;
   }
}

public class LinkedList<K,T> where K : IComparable<K>,new() 
// K must implement Icomparable<K> and have default constructor

Reference/Value Type Constraint

Sometimes, you need to define constraint so that the generic data type is value type or reference type.

You can constrain a generic type parameter to be a value type (such as an int, a bool, and enum) or any custom structure using the struct constraint:

public class MyClass where T : struct

Similarly, you can constrain a generic type parameter to be a reference type (a class) using the class constraint:

public class MyClass where T : class

Inheritance using generics

public class SubClass<T> : BaseClass<T>
// SubClass and BaseClass will have generic type T
public class SubClass<T> : BaseClass<T> where T : ISomeInterface
// SubClass and BaseClass will have generic type T which implements ISomeInterface
public class SubClass<T> : BaseClass<T> where T : new() 
// SubClass and BaseClass will have generic type T which must have default constructor

Generic Methods

public class SubClass<T> : BaseClass<T>
{
   public override T SomeMethod(T t) {}
   // BaseClass and SubClass will have generic type T and SomeMethod() will take parameter T and return object of type T
}

Hope this helps you to understand Generics in C#.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adarsh Chaurasia - Enjoying Full Stack Development
Software Developer (Senior)
India India
 Innovative & tech-savvy software development professional wit7 6+ years of progressive experience specializing in object-oriented approach to enterprise commercial solution delivery utilizing latest technologies.
 Extensive background in Full Life-Cycle of Software Development and Automation process including requirements gathering, design, coding, unit testing, automation, debugging and maintenance.
 Comprehensive knowledge of Relational Database technology, queries and procedures.
 Excellent understanding of OOPS Concepts, Design Patterns & Best Coding Practices.
 Strong architectural knowledge of designing n-tier and n-layer solutions using Application blocks, Enterprise library, SOA, Software factories.
 Working experience of IIS administration and website deployment and configuration.

Strengths & Area of expertise include:

 Full Stack Enterprise Web Development
 Multi-Threaded Programming
 Object Oriented Development
 REST APIs Development & Integration
 Distributed/Client Server System
 TDD & Behavior Driven Development (BDD)
 Database Design & Development
 Exceptional Analytical & Quick Learning Skill
 Team & Client Communication
 Proactive & Organization Development Attitude

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
AJEET KUMAR22-Aug-15 6:25
memberAJEET KUMAR22-Aug-15 6:25 
GeneralNot explained properly Pin
Rhutesh K21-Nov-14 3:34
memberRhutesh K21-Nov-14 3:34 
GeneralRe: Not explained properly Pin
Adarsh Chaurasia (Consultant|Mentor|Tech Savvy)25-Nov-14 5:25
memberAdarsh Chaurasia (Consultant|Mentor|Tech Savvy)25-Nov-14 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 28 Feb 2014
Article Copyright 2014 by Adarsh Chaurasia - Enjoying Full Stack Development
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid