Explain non-professional Generics in C #?
Duplicate What is the "<>" Syntax Inside C #
In fact, I want to know, "why and when should I use generics?".
What is the need?
Generics are a way to provide type safety during compilation in C #.
Example: preliminary data:
class Person { string name; string lastname; public Person(string _name ) { this.name = _name; } } class ClientCode { public static void Main() { //create a list of person ArrayList personList = new ArrayList(); Person p1 = new Person("John"); Person p2 = new Person("Ram"); personList.Add(p1); personList.Add(p2); // BUT, someone can do something like: // ArrayList does not stop adding another type of object into it object q = new object(); personList.Add(q); // while accessing personlist foreach(object obj in personlist) { Person p = obj as Person; // do something, for person // But it will fail for last item in list 'q' since its is not person. } } } Example-Post-Generics:
class ClientCode { public static void Main() { //create a list of person List<Person> personList = new List<Person>(); Person p1 = new Person("John"); Person p2 = new Person("Ram"); personList.Add(p1); personList.Add(p2); // Someone can not add any other object then Person into personlist object q = new object(); personList.Add(q); // Compile Error. // while accessing personlist, No NEED for TYPE Casting foreach(Person obj in personlist) { // do something, for person } } } Generalizations allow you to parameterize code types by type in the same way as arguments that allow you to parameterize it by value. This probably doesn't explain much, so it allows you to go step by step:
Imagine that you want the program to print "I like rabbits." You write this:
static void Main() { ILikeBunnies(); } void ILikeBunnies() { Console.WriteLine("I like bunnies"); } All is well and good. But you also love cheese, so now you have:
static void Main() { ILikeBunnies(); ILikeCheese(); } void ILikeBunnies() { Console.WriteLine("I like bunnies"); } void ILikeCheese() { Console.WriteLine("I like cheese"); } You notice that your two functions are almost identical. You want to reuse the same function, but specify different values ββfor what you like:
static void Main() { ILike("bunnies"); ILike("cheese"); } void ILike(string what) { Console.WriteLine("I like " + what); } These are function arguments: they allow you to reuse the same code with different values.
Generics are like that, but instead of passing in different values, you pass types. Suppose you have code that must bind two strings into an array:
static void Main() { string[] s = Pair("a", "b"); } string[] Pair(string a, string b) { return new string[] { a, b }; } Lovely and dandy. Later, you realize that you also need to bind ints to an array:
static void Main() { string[] s = Pair("a", "b"); int[] i = Pair(1, 2); } string[] Pair(string a, string b) { return new string[] { a, b }; } int[] Pair(int a, int b) { return new int[] { a, b }; } As before, we see there is an excess of redundancy. We need a function that returns a couple of all and a way to pass the type of what we want. Here's what generics are:
static void Main() { string[] s = Pair<string>("a", "b"); int[] i = Pair<int>(1, 2); } T[] Pair<T>(T a, T b) { return new T[] { a, b }; } Angle brackets allow you to pass a type to a function in the same way that you can pass values ββin brackets. The "T" here is the name of the type argument, as the "what" was the argument of the value above. Any place T that appears in the function will be replaced by the actual type you are passing (string and int in the example).
There are tons of things you can do with generics outside of this, but the basic idea is that generics allow you to pass types to functions (and classes) in the same way that arguments let you pass values.
Generics basically eliminates the need to throw objects into their base type.
eg. if you want to keep the Foos group on the list.
Previously, you had to create your owen FooList or throw an element as objects.
All this takes you time and a compliment.
With Generics, all you have to do is sat List, which checks your types and speeds up your programs. (without boxing and unpacking)
Suppose that you yourself are an algorithm that can sort any objects that can be compared with a pair (playing cards, CDs, business cards, etc.). You really are not interested in the fact that these specific objects are provided, you can compare them. This way you become generic (here "generic" is used in a broad sense, not in the sense of C #).
Generics in .NET helps to simplify this type of behavior not only in terms of algorithms (common functions), but also in terms of common types (classes, structures, delegates, interfaces).
I wrote about this for a long time, here . I was working with XML, and I wanted the helper to get XmlElement or XmlAttribute (based on XPath) and let me work with it. This was a good simple example that I worked with in the real world when generics were fairly new to C #.