C # Generic Foundation

Source: Internet
Author: User

  1. Generics are a new feature in c#2.0, which enhances performance, makes code more expressive, provides a better generalization, and is implemented in earlier versions of C # by casting between types and universal base type Object, which provides a solution to this limitation. It also transfers a large amount of security checks from execution to compile time. In C #, you can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegates, but there are no generic properties, indexers, operators, constructors, and destructors.
  2. Constraints on type parameters
  3. Constraints Description

    t:struct

      nullable  Any value type other than . For more information, see Working with Nullable Types (C # Programming Guide).

    t:class

    T:new ()

    The type parameter must have a public constructor with no parameters. When used with other constraints, thenew () constraint must be specified last.

    t: base class name

    t: Interface name

    t:u

    The type parameter provided for T must be either a parameter supplied for u or a parameter derived from U. This is called a bare type constraint.

  4. Generic types can be overloaded, such as MYTYPE,MYTYPE<T>,MYTYPE<T,U&GT, and these are different types, and they cannot be converted to each other, which is also true for generic methods,
    // in the same class void DoWork () {} void Dowork<t>() {}void dowork<t, u> () {}
    The constructors of generic classes do not have angle brackets,
    Dictionary generic class Public classDictionary<tkey, tvalue>: Idictionary<tkey, Tvalue>, Icollection<keyvaluepair<tkey, TValue> Ienumerable<keyvaluepair<tkey, Tvalue>&gt, IDictionary, ICollection, IEnumerable, ISerializable, Ideserializationcallback { PublicDictionary (); PublicDictionary (Idictionary<tkey, tvalue> Dictionary); PublicDictionary (iequalitycomparer<tkey> comparer); PublicDictionary (intcapacity); PublicDictionary (Idictionary<tkey, tvalue> Dictionary, iequalitycomparer<tkey> comparer); PublicDictionary (intcapacity, iequalitycomparer<tkey> comparer);protectedDictionary (SerializationInfo info, StreamingContext context); 。。。。。。 }
  5. Type inference for generic method type arguments: Type inference applies only to generic methods, not generic types,
    Static List<t> makelist<t>(T first,t second) ... List<string>list  = makelist<string> ("Tom", " Jerry " ); // use type deduction to write list<string>list=makelist ("Tom","Jerry ")
    For generic classes, you can define a generic method implementation type deduction in a class
     Public class Pair<t1,t2>:iequatable<pair<t1,t2>>{...  publicstatic pair<t1,t2>of<t1,t2>(T1 first,t2 second) {return New Pair<t1,t2>(First,second);}}
  6. Covariance, contravariance, and invariance in generics:
      • Covariance: Generic type parameters can be converted from derived classes to base classes, and this change is harmonious. Typically, for delegates, covariant type parameters can be used as the return type of a delegate, and for an interface, the covariant type parameter may be used as the return type of the interface's method. Interfaces that support covariance have ienumerable<t>,ienumerator<t>,iqueryable<t>,igrouping<tkey,telement>
      • Inverse: A generic type parameter can be implicitly converted from a base class to a derived class. Typically for delegates, contravariant parameters can be used as parameter types, for interface contravariant type parameters can be used as parameter types of interface methods, interfaces that support Contravariance have icomparer<t>,icomparable<t> and IEqualityComparer <T>
      • Invariant: Indicates that a generic type parameter is neither a covariant type nor an contravariant type, only the specified fixed type can be used
  7. C#4.0 can use the out modifier to specify covariance of type parameters, using the inverse of the in modifier type parameter
  8. Issues to be aware of variability in the CLR:
      • In the. NET Framework 4, the variability type parameters are limited to generic interfaces and generic delegate types.
      • A generic interface or generic delegate type can have both covariant and contravariant type parameters.
      • Immutability applies only to reference types, and if you specify a value type for a variable type parameter, the type parameter is constant for the resulting constructed type.
      • Variability does not apply to delegation combinations. That is, in the case of two delegates of the given type action<derived> and action<base>, the second delegate cannot be combined with the first delegate, although the result will be type-safe. Variability allows the second delegate to be assigned to a variable of type action<derived>, but can be combined only if the type of the two delegates exactly matches.

C # Generic Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.