Introduction to Generics

Have you ever used List class in your program?  Did you noticed strange <> brackets in the syntax, brackets where you can pass  any type of data? Well, that’s example of practical use of generics.

Generis are used to delay specification of data type, means that allows you to write a class or method that can work with any data type. In this post I’ll be using C#.

Lists

Generics are implemented from System.Collections.Generic” namespace. List is probably the most popular Class that inherits from this namespace.

For people unfamiliar with lists: List  represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. Lists are similar to arrays, both are created to store data of the same type in one place of memory, where we can add new item or find item by index. but have a lot of That’s enough of theory lets take a look at examples:

class Program  
{
    static void Main() 
{
	List<int> list = new List<int>();   //Creating our list
	list.Add(2);                         // Adding new items
	list.Add(3);
	list.Add(5);
	list.Add(7);
    }
}

In this example we create a simple list of integers. Lists comes with a predefined methods such as Add(), Remove(), that makes life a lot of easier, but let’s back to the generics…

List<T>()

<T> represents data type, that means you can create list of integers, characters etc..

Functions

Let’s move on to the functions

 class CreateArrayOfChosenData<T>             // Creating new Class
 {
    private[T] array;

    static CreateArrayOfChosenData(int size//Creating constructor of this class
     {
          array =  new T[size+1];           // Array indexes starts with 0
 }
 }

We are creating simple class that will produce an array of chosen data type and length

CreateArrayOfChosenData<Int> = new CreateArrayOfChosenData<Int>(10);

Here we are creating an instance of our class and passing data type as Int, and  10 as a number of items. Here you can see difference of how each bracket works.

Methods

Generics can also be used in methods. In this simple example we will simply pass values to the method and print the result.

 

class MyClass
 {
     static void CreateAnyTypeofObjects<T,U> (T type1,U type2)
     {
     T object1=type1;
     U object2=type2;
     Console.WriteLine("first object ={0}, secound object ={1}",object1,object2);

    }

We can see that we can create more than one type of object to select, and perform operations on passed objects. This method only illustrates the core structure of creating method with generics fields.

In the next part I’ll try to explain generics in concept of delegates, generic types of return. and functions Func<>,Action<>.

Author: indiedevart

Indie Games Developer

12 thoughts on “Introduction to Generics”

  1. I’m curious to find out what blog platform
    you are working with? I’m experiencing some small security problems with my latest site and I’d like to
    find something more risk-free. Do you have any solutions?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s