Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anybody give a good example of what to use generic classes for?

Tags:

c#

class

generics

We recently learned about generic classes in C#, but our teacher failed to mention what they can be used for. I can't really find any good examples and would be extremly happy if somebody help me out :)

Have you made your own generic class, and what did you use it for?

Some code examples would make me, and my classmates, really happy! Pardon the bad english, I am from sweden :)

happy programming!

Sorry- I think I could have written the question a bit better. I am familar with generic collections. I just wondered what your own generic classes can be used for.

and thank you for the MSDN links, I did read them before posting the question, but maybe I missed something? I will have a second look!

like image 768
Iris Classon Avatar asked Oct 07 '11 14:10

Iris Classon


People also ask

What are generic classes used for?

Generic classes and methods combine reusability, type safety, and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. The System. Collections.

What are generics and what are they useful for?

Generics enable the use of stronger type-checking, the elimination of casts, and the ability to develop generic algorithms. Without generics, many of the features that we use in Java today would not be possible.

What is generic class in Java with example?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.


1 Answers

Generic Collections

Generics for collections are very useful because they allow compile time type safety. This is useful for a few reasons:

  • No casting is required when retreiving values. This is not only a performance benefit but also eliminates the risk of there being a casting exception at runtime

  • When value types are added to a non generic list such as an ArrayList, the value's have to be boxed. This means that they are stored as reference types. It also means that not only does the value get stored in memory, but so does a reference to it, so more memory than necessery is used. This problem is eliminated when using generic lists.

Generic Classes

Generic classes can be useful for reusing common code for different types. Take for example a simple non generic factory class:

public class CatFactory
{
  public Cat CreateCat()
  {
    return new Cat();
  }
}

I can use a generic class to provide a factory for (almost) any type:

public class Factory<T> where T : new()
{
  public T Create()
  {
    return new T();
  }
}

In this example I have placed a generic type constraint of new() on the type paramter T. This requires the generic type to contain a parameterless contructor which enables me to create an instance without knowing the type.

like image 130
Charlie Avatar answered Oct 02 '22 02:10

Charlie