Summary of generic Program Design in C #

Source: Internet
Author: User

Today I have summarized the C # Generic program design, and I think the so-called generic Program Design in C # Corresponds to the template in C ++,

The functions and mechanisms of generic design are very similar. In C ++, there are "function templates" and "class templates ", in C #, there are corresponding "generic methods" and "generic methods ".

Type ".

A: Let's talk about the "generic method" first. The generic method in C # refers to the method member who uses the type parameter,

For example:

Public static void Swap (Ref T x, ref T y)
{
T tmp = x;
X = y;
Y = tmp;
}

Int a = 1;
Int B = 2;
Swap (Ref a, ref B); // call the generic method to specify the specific type for the parameter type

Swap (ref a, ref B); // call the generic method. The C # compiler automatically determines the specific type of the parameter type based on the parameters passed to the method.


When using the generic method, you must specify the following points:

1. generic methods can also impose type restrictions on their parameter types, including: main restrictions, secondary restrictions, and function restrictions. (For details about type restrictions, refer to the following generic classes)

Ii. in C #, special functions such as constructor, attribute, event, index function, and operator cannot be defined as generic methods.

3. generic methods can also be defined as virtual methods, overload methods, abstract methods, or sealing methods. generic methods can also achieve override overloading. Of course, they can also be used.

Implementation of polymorphism. When a base class is overloaded with generic methods in the base class, all types of generic methods in the base class are automatically inherited, and there is no need to declare them again.

4. generic methods can also be called through the delegation mechanism. There are two implementation mechanisms: "common delegation" and "generic delegation ",

For example:


Public static void Swap (Ref T x, ref T y)
{
T tmp = x;
X = y;
Y = tmp;
}

Public static void Clear (Ref T x, ref T y)
{
X = default (T );
Y = default (T );

}

Normal Commission:

Public delegate void fun (ref int x, ref int y );

Int a = 1;
Int B = 2;

Fun fun1;
Fun1 = Swap ;
Fun1 (ref a, ref B );


Generic delegation:

Public delegate void Fun (Ref T x, ref T y );

Fun Fun2;
Fun2 = Swap ;
Fun2 (ref a, ref B );


B: Generic and generic Interfaces

For example:

Class LinkNode
{
Protected T data;
Protected LinkNode Next;
// The linked list structure is directly formed by storing the next object without using pointers.
Public T Data {
Get {return data ;}
Set {data = value ;}
}
Public LinkNode Next {
Get {return next ;}
Set {next = value ;}
}


Public LinkNode () // No-argument Constructor
{
// Use the keyword default to obtain the default value of the "abstract" variable
Data = default (T );
}
Public LinkNode (T t) // constructor with Parameters
{
Data = t;
}
// Move n nodes backward
Public static LinkNode Operator> (LinkNode Node, int n)
{
LinkNode Node1 = new LinkNode ();
Node1 = node;
For (int I = 0; I {
Node1 = node. next;
}
Return node1;
}
Public static bool operator = (LinkNode T1, LinkNode T2)
{
// Return (t1.Data = t2.Data );
// The error cannot apply the comparison character to the T type of the unknown type.
// However, you can directly use the comparison methods in the object class, such as Equals ();
Return (t1.Data. Equals (t2.Data ));
}

}

In the preceding example, note the following points when writing a generic class or generic interface:

1. The name of the constructor of a generic class is the same as that of a class, and does not include a type parameter.

2. Comparison operators and other operators cannot be applied to objects of type parameters in generic classes at will, which will certainly report errors, however, if you use the related law in the object class, it is a line.

3. Multiple generic parameters can be used in a generic class.

4. generic classes cannot own anything. Only the constructor type of generic classes and their specific instances can have member methods or data members. In C #,

The member belongs to the construction type of the generic class.

5. You can add type restrictions when writing generic classes, generic interfaces, or generic methods, that is, C # allows you to use the where keyword to restrict type parameters during generic definition.

Methods include: Primary restrictions (which can be a struct value type or a class reference type) and secondary restrictions (restrictions that must be inherited from the specified base class or a few ports), constructor restrictions

(The target type of the type parameter must provide a non-argument constructor.) I personally think the type restrictions here actually provide great convenience in C # programming, this is a template in C ++.

But it also sacrifices some flexibility.

6. There is a basic principle in the inheritance of generic classes caused by generic classes: "Open type cannot be used as the base class of closed type". To put it bluntly, that is

When a common class inherits a generic class, a non-generic common class cannot directly inherit from a generic class, but can inherit from a closed constructor of a generic class. My personal summary is the "openness" of the class in the Process of inheritance"

It cannot be smaller, but it can remain unchanged or become larger.


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.