Document directory
- What is generic
- Why do we need generics?
- What are the advantages of generics over traditional collection classes?
- Refer:
What is generic
The so-called generic type can be any type. The most common generic type is used to create a set.
1: List<int> list = new List<int>();
2: list.Add(1);
Why do we need generics?
This problem can be divided into several branches. Let's think one by one.
1: int result = 0;
2: foreach (int i in list)
3: {
4: result +=i;
5: }
- We can perform operations on generics. For ArrayList, we are not so convenient. In this way, errors are not prompted for writing ArrayList, but errors will occur during running. It brings potential risks to program robustness.
1: foreach (int i in array)
2: {
3: result += i;
4: }
-
C. code reuse
- In the past, if we wanted to define a strongly typed set, the most common method would be to inherit from ICollection.
1: public class StrongTypeCollection : ICollection
2: {
3: public void Add() { // TODO:: Implement this }
4:
5: // Other Methods To Override
6: }
- Now we can use IList <int> myCollection = new List <int> (int capbility );
1: public class MyClass : IComparable<int>, IComparable<double>
2: {
3: }
Generic usage
1. Collection class definition, 2. generic type, 3. Generic Method 4. Generic proxy
But before that, let's take a look at the restrictions on the use of generics, that is, constraints on generics. There are 5 types of constraints for generics
Value Type Constraints |
Class MyClass <T> where T: struct |
Reference Type Constraints |
Class MyClass <T> where T: class |
Interface Type Constraints |
Class MyClass <T> where T: IComparable <int> |
Base class constraints (only subclasses inherited from BaseClass can be converted to generic types) |
Class MyClass <T> where T: BaseClass |
No parameter constructor Constraints |
Class MyClass <T> where T: new () When there are multiple types, new () can only be placed at the end Class MyClass <T> where T: class, new () |
The above example is a generic type. Multiple types can be defined in. Net.
1: // define multiple types
2: public class MyClass<U, T>
3: // define multiple types and determine the type constraints
4: public class MyClass<U, T>
5: where U : struct
6: where T : class, new()
7: // determine the relationship between types
8: public class MyClass<U, T> where U : T
The last line of code indicates that U must inherit from T. See the base class constraints in the above table.
We will give an example of the above three usage methods.
Collection class definition
1: IList<MyStruct> list = new List<MyStruct>();
Generic Type
1: public class MyItem : IComparable<MyItem>
2: {
3: public int CompareTo(MyItem item)
4: {
5: return this.Value.CompareTo( item.Value);
6: }
7:
8: public int Value {get;set;}
9: }
Generic Method
1: using System;
2: using System.Collections.Generic;
3: public class MyClass
4: {
5: public static void Main()
6: {
7: MyGeneric my = new MyGeneric();
8: my.Run<double>(3);
9:
10: // The Type Must be the concerate generic type
11: // my.Run<double>(new Random());
12:
13: Console.Read();
14: }
15: }
16: public class MyGeneric
17: {
18: public void Run<T>(T t) where T : struct
19: {
20: Console.WriteLine("The Value Here Is {0}", t);
21: }
22: }
Generic proxy
1: public delegate void MyAction<T>(T t); // Define the Type
2: public event MyAction<string> OnActing; // Define The Event Instance
3:
4: public void Output(string message)
5: {
6: Console.WriteLine(message);
7: }
8:
9: public void TestGenericDelegate()
10: {
11: OnActing += Output; // Register A Event
12: OnActing("Hello, Generic Methods"); // Call The Event
13: }
There are also a few small snacks with the. Net wildcard features.
1. default (T): determines the default type for the generic type. The reference type is NULL, and the value type is default, for example, int is 0.
2. Compare the size
1: public class MyClass<T>
2: {
3: public T Value {get;set;}
4: public int CompareTo(T t)
5: {
6: return this.Value - t,Value; // Wrong
7: }
8: }
9:
10: public class MyClass<T> where T : IComapreable<T>
11: {
12: public T Value {get;set;}
13: public int CompareTo(T t)
14: {
15: return this.Value.CompareTo(t,Value); // Correct
16: }
17: }
3. Special processing for generics in reflection and special processing for generics in reflection. Click here to view details.
4. Do not Share Static Members among Generic classes. For details, see Generic Types Don't Share Static Members.
Refer:
Http://msdn.microsoft.com/en-us/library/512aeb7t.aspx
Generic Types Don't Share Static Members
All the articles in this blog are shared by creation. You can reprint, excerpt, and modify them to your own version. However, if you want to use this article for commercial purposes, contact the author, thank you for your cooperation.