c # study notes - Generic (Generic)

First, what generics are?

1, the operation is implemented in a variety of data types on the same code parameter types. Using a "parameterized type," the abstract type, thereby realizing flexible multiplexing.

In other words, it allows you to write generic class or method can work with any data type.

2, the C # language generic method is a parameter by the generic type constraint method, it can be understood as the data type of the parameter set.

If there are no generics, each method parameter type is fixed and can not be altered.

After using generic data type method, there are constraints to specify generic, i.e. can be delivered according to different types of generic parameters provided.

Required between the generic method definition method name and parameter list together <>, and wherein Tto represent the parameter type.

Of course, other identifiers may be used instead of the parameter type, but usually are used Tto represent.

Second, examples

[Example] create a generic method to achieve the summation of the two numbers.

The subject of the request, as follows.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // The type T is set to double
  6. Add<double>(3.3, 4);
  7. // T is set to be an int
  8. Add<int>(3, 4);
  9. }
  10. // adding
  11. private static void Add<T>(T a, T b)
  12. {
  13. double sum = double.Parse(a.ToString()) + double.Parse(b.ToString());
  14. Console.WriteLine(sum);
  15. }
  16. }

Above code is executed, results as shown in FIG.

Using generic methods


Implementation of the results can be seen from the above, different parameter types can be specified addition operations performed when call the Add method.

If the call the Add method, no arguments are passed in accordance with a predetermined type <T>, a compilation error occurs, so that you can avoid abnormal program at run time

III characteristic, generics

The use of generics is a technique to enhance the function of the program, specifically in the following areas:

  • It helps you maximize code reuse, type of protection and safety to improve performance.
  • You can create a generic collection classes. .NET Framework Class Library System.Collections.Generic contains some generic collection classes new namespace. You can use these generic collection classes instead System.Collections collection classes.
  • You can create your own generic interfaces, generic classes, generic methods, generic events and generic delegate.
  • You may be constrained generic class in order to access specific data types.
  • Generic type of data regarding the type of information is available for use at runtime by using reflection.

Fourth, in a statement generic method / generic class, you can add to the generic certain constraints to meet some of our specific conditions.

such as:

using System;
using System.Web.Caching;

namespace Demo.CacheManager
{
   public class CacheHelper<T> where T:new()
   {
      
   }
}

Generic qualification:

  • T: Structure (must be a value type parameter can specify the type Nullable any value other than the type.)
  • T: class (type parameter must be a reference type, including any class, interface, or delegate array type)
  • T: new () (. Parameter Type parameter must have free public constructor new () constraint must be specified last, when used in conjunction with other constraints)
  • T: <base class name> class type parameter must be specified group derived from a base class, or specified
  • T: <Interface name> Type parameter must be specified interface or implement the specified interface. Constraints can specify multiple interfaces. Constraints interface may also be generic.
  • T: U

Guess you like

Origin www.cnblogs.com/AmbitionBoy/p/12013738.html