C # Generic-create your own generic

Source: Internet
Author: User

Generic is a feature of programming languages. In a strong-type language, a type can be specified only when a program is designed without specifying a type.

To put it bluntly, a method must support parameters of the int, decimal, string, and other types, with overload? There are too many reloads. Are object-type parameters used? It is a waste of resources to unpack the box or forcibly convert the type, and the type check may not be performed during compilation. In addition, I want to return different type values using the same method.

C # Using Generics can solve these problems. C # generic types can be used for classes, structures, interfaces, delegates, and members.

Generic examples

Using system. Collections. Generic;


List <int> L1 = new list <int> ();

List <string> L2 = new list <string> ();


L1.add (1 );

L2.add ("");


M = L1 [0];

A = L2 [0];

It can be seen that the list can easily process the int type and the string type through generics.

First generic Method

Public Class C

{

Public t Foo <t> (T value)

{

Return value;

}

}



C = new C ();
Int I = C. Foo <int> (3) + 2;
Decimal d = C. Foo <decimal> (30) + 20;

MessageBox. Show (I. tostring () + "\ r \ n" + D. tostring ());

Int J = C. Foo (3); // It is also correct, and the compiler will automatically infer the type.

The above implements a generic type (although the code of the foo method is extremely simple), the type of parameters and return values is specified by specifying Foo <t>.

Someone may ask:

  • Does it have to use the t letter? Can I use T1? Yes, you can.
  • Is T type not required for parameters, and T type is required for return values? No, but you can specify the int type for the parameter based on your needs. You can also specify the return value as the string type, or do not return the value.

Use multiple generics at the same time

Public void Foo <t1, T2> (T1 value1, T2 value2, int value3)

Default Value

Because the generic type does not know the type in use, it does not know whether it will be a reference type or a value type (Value Type and structure type ). We want to assign a null initial value to the variable for the reference type and a 0 initial value to the variable for the value type. The default keyword is used.

T = default (t );

Generic is not an object

The generic is not an object and does not involve packing or unpacking operations. Therefore, the following code is incorrect:

Public int Foo <t> (T value)

{

Return (INT) value;

}

Public t Foo <t> (T value1, t value2)

{

Return value1 + value2;

}

Generic Type

The generic method is introduced earlier. In fact, the creation of generic classes, generic interfaces, and generic delegation is similar to the generic method.

Public Class C <t>

{

Public t value1 {Get; set;
}

Public t value2 {Get; set;
}


Public C (t value1, t value2)

{

Value1 = value1;

Value2 = value2;

}


Public void swap ()

{

T = value1;

Value1 = value2;

Value2 = T;

}

}


C <int> C1 = new C <int> (1,
2 );

C <string> C2 = new C <string> ("A", "B ");


C1.swap ();

C2.swap ();

MessageBox. Show (c1.value1. tostring () + c1.value2. tostring () + c2.value1 + c2.value2 );

Http://www.cftea.com/c/2012/04/5535.asp

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.