About generic and generic

Source: Internet
Author: User

About generic and generic

1. Generic

Inheritance:

All types of parent classes are object objects. All types of base class subclasses inherit everything from the parent class (the principle of replacement by the RYS)

 


Execution method:


Each class of the constructor has a default constructor. A class can have multiple constructor. When executing the constructor, each class (if it inherits the parent class) the sub-classes will first execute the parent class constructor and then execute their own functions, so that the sub-classes can have the attributes and methods of the parent class.
Application scenarios such as writing a general generic method and passing in a class as the type. In this way, you can use the method attributes of this class and some inherited attributes and methods.
Public static T Way <T> (T para) where T: People, new (){}

Object. gettype () -----> get the type of the object
Typeof () ---- typeof (int) specifies the type in it
Gettype and typeof are for objects and types respectively.


Declaration ------------ generic <T> (T value)
Latency statement
Declared static can be a direct class name. the method name can be called without the need of new. Generally, it is written as static in public, and the static member information (attributes) will be allocated to the memory worker (whether int or string) theoretically, int is allocated to the memory heap. string is allocated to the memory worker.
Class. method name. <int> (value) Here value must be int type

Generic constraints where T: class, Interface, new () where the relationship is the same. Is the constraint on the objects passed in to the generic set

Advantages of generics: reduces code redundancy. Compared with the object type, there is no unpacking (unpacking object -- "int" converts the reference type to the value type, packing ---- convert the value type to the reference type)
Value Type: the stack is allocated from the stack of the thread. The garbage collection mechanism is inherited from the System. valueType, which can be boxed or unboxed. It cannot be used as a base class or as a virtual method. It is set to 0 by default during initialization and is copied as a field.
Reference Type: allocated from the managed heap. The object is considered as a garbage collection mechanism. All classes are referenced and can be inherited and derived only in the packing format. The default value of the referenced type variable is null during initialization, only the memory address is copied.

Related Article

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.