Introduction to asp. NET generic programming. NET generic programming (1/9)

Source: Internet
Author: User

Preface

The emergence of generics in. net 2.0 is an exciting feature. But what is generic? Do you need them? Will you use them in your own applications? In this article, we will answer these questions and carefully analyze the usage, capabilities, and limitations of generics.

Type security

Many languages in. net, such as c #, c ++, and vb.net (the option strict is on) are strongly-typed languages. As a programmer, when you use these languages, you always expect the compiler to perform type security checks. For example, if you convert a reference to a book type into a vehicle reference, the compiler will tell you that such cast is invalid.

However, when talking about collections in. net 1.0 and 1.1, they do not help type security. Consider an arraylist example, which has an object set-this allows you to store any type of objects in this arraylist. Let's take a look at the code in example 1.

Example 1. Lack of type-safe arraylist

Using system;
Using system. collections;
Namespace testapp
{
Class test
{
[Stathread]
Static void main (string [] args)
{
Arraylist list = new arraylist ();
List. add (3 );
List. add (4 );
// List. add (5.0 );
Int total = 0;
Foreach (int val in list)
{
Total = total + val;
}
Console. writeline ("total is {0}", total );
}
}
}

In this example, we create an arraylist instance and add 3 and 4 to it. Then, I cyclically traverse the arraylist, retrieve the integer values, and add them together. This program will generate the result "total is 7 ". Now, if I comment out the following sentence:

List. add (1, 5.0 );

The program will generate the following runtime exceptions:

Unhandled exception: system. invalidcastexception: specified cast is not valid.
Attestapp. test. main (string [] args) in: workareatestappclass1.cs: line 17

What went wrong? Remember that arraylist has an object in a set. When you add 3 to the arraylist, you have packed the value 3. When you loop through the list, you split the element into int type. However, when you add a value of 5.0, you are packing a double value. In row 17th, the double value is split into an int type. This is the cause of failure.

Note: If the above example is written in vb.net, it will not fail. The reason is that vb.net does not use the packing mechanism. It activates a method to convert the double type to an integer type. However, if the value in the arraylist cannot be converted to an integer, the vb.net code will fail.

As a programmer who is used to the type security provided by the language, you want such problems to emerge during compilation rather than during runtime. This is why generics are generated.

3. What is generic?

Generics allow you to implement type security during compilation time. They allow you to create a data structure but not limited to a specific data type. However, when this data structure is used, the compiler ensures that the type used is consistent with the type security. Generics provide type security, but do not cause any performance loss or code bloated. In this regard, they are similar to the templates in c ++, but they are quite different in implementation.

Home 1 2 3 4 5 6 7 8 9 Last page

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.