C # understanding and optimization of generic and strong and weak types,

Source: Internet
Author: User

[Conversion] c # understanding and optimization of generic and strong and weak types,

[Concept of generics]
(1) When there is no generic type, all objects are based on objects. If you want to use it, you must perform forced type conversion. If you want to use the value type, this will lead to the process of constantly unpacking and packing, which will consume a lot of system resources.
(2) When using generics, you do not need to use the object class for packing and unboxing. They are all determined types.
(3) The concept of generics is introduced by the set at the beginning, because the types in the set are based on the object class. You can use generic classes in the collection.
(4) generics are not only used for collections, but also for delegation, interfaces, and methods.
Advantages of generic: High Performance
ArrayList list1 = new ArrayList ();
List1.Add (44); // boxed
Int il1 = (int) list1 [0]; // unpack
Foreach (int i2 in list1)
Console. WriteLine (i2); // execute unpacking

Frequent unpacking and packing operations are performed repeatedly when there is no generic type, which consumes a lot of system resources. You can use a generic set.

List <int> list2 = new List <int> ();
List2.Add (44); // do not pack
Int il2 = list2 [0]; // do not unpack
Foreach (int i2 in list2)
Console. WriteLine (i2); // do not unpack

When defining a generic type, the value type and reference type are distinguished.

Advantages of generic: type security
ArrayList list = new ArrayList ();
List. Add (44 );
List. Add ("mystring ");
List. Add (new MyClass ());
Foreach (int I in list)
Console. WriteLine (I );

Type security is to avoid exceptions in advance.

List <int> list2 = new List <int> ();
List2.Add (44 );
// List2.Add ("mystring ");
// List2.Add (new MyClass ());
Direct compilation fails.


Advantages of generics: code reuse and extension
List <T> This generic class can be instantiated as needed:

List <int> list = new List <int> ();
List. Add (44 );

List <string> stringList = new List <string> ();
StringList. Add ("mystring ");

List <MyClass> myclassList = new List <MyClass> ();
MyClassList. Add (new MyClass ());

Advantages of generics: code reuse and extension

Define a generic class:
Public class aaa <T> Note: here T is only an identifier and can be defined as any character.
{
Public void abc (T)
{
Console. WriteLine ();
}
}

Use it:
// Instantiate with string
Aaa <string> aaa = new aaa <string> ();
Aaa. abc ("aaabbb ");
// Use int for instantiation
Aaa <object> bbb = new aaa <object> ();
Bbb. abc (new object ());
Console. Read ();
Naming Conventions
(1) The name of the generic type uses the letter T as the prefix.
(2) When using generics, use <T>, for example:
Public class List <T> {}
Public class complete list <T> {}
(3) If a generic type has specific requirements (for example, an interface derived from a base class must be implemented) or two or more generic types are used, A descriptive name should be used for the generic type:
For example:
Public interface ccc <TTT>
{Void abc (TTT arg1 );}
Public class aaa <TTT>: ccc <TTT>
{Public void abc (TTT)
{Console. WriteLine ();}}

Public class SortedList <TKey, Tvalue> {}

Generic set
Generic Collections exist in using System. Collections. Generic. We mainly introduce the following List <T>. For other Generic Collections, see MSDN.

 

 

Strong and weak types refer to two classes with direct or indirect inheritance relationships. If a class is a direct or indirect base class of another class, it is a weak type, and the direct or indirect subclass is a strong type. The Foo and Bar classes used in subsequent introductions are defined here. Bar inherits from Foo. Foo is a weak type, while Bar is a strong type.

With the concept of strong and weak types, we can define co-Variation and inversion in this way: if the type of TBar is based on the type of strong Bar, the TFoo type is based on the Foo type of the weak type. The covariant is to assign the TBar type instance to the variable of the TFoo type, the inverter assigns a TFoo-type instance to a variable of the TBar type.

Use of coordination and inverter in Delegation

The coordination and inverter are mainly reflected in two aspects: interface and delegation. First, let's take a look at how to use the coordination and inverter in the delegation. Now we have defined a generic delegate Function that represents a Function without parameters. The type parameter is the type of the Function return value. An out keyword is added before the generic parameter to indicate that T is a covariant. During use, the forced-type delegated Fucntion instance can be assigned a value to the weak-type delegated Fucntion variable.

 

[Weak type]

C # is a strongly typed language. Generally speaking, it means that the "variable" type in C # is explicit during development: String is String, Int32 is Int32, and there is no dispute. There are many advantages of the strong type, so you can take the following examples at will:

Code prompt
Support for refactoring tools
More errors can be found during compilation
However, C # is not an "absolute" strong type language, because it also has a weak type, that is, the Object. We know that Object is the final base class of all types, and any type of Object can be referenced by Object. However, once the variable is converted into an Object, the code prompt disappears. Even if we "specify" the exact type of the Object, it must also be used through Cast-not to mention it forms an opportunity to be "abused" or "misused. For example, an error code may pass in an object that does not conform to the agreed type, which may cause an error. More seriously, such errors may only be detected at runtime, And the compiler cannot do anything about them.

From: http://hi.baidu.com/morespring/blog/item/fe9220358a03690aebc4afa4.html

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.