Profiling the creation and use of C # 2.0 generic classes

Source: Internet
Author: User
Tags define object definition data structures implement key net visual studio

"One code, multiple use," which is the root of the introduction of generics. Known as templates in previous C + +, C # generics support independent encoding through algorithms and data structures. For example, a generic list means that you do not have to rewrite a strong-type collection again. In this article, the author will show you how easy it is to define and use generics-note that generics have long been considered the most advanced and difficult term.

First, Introduction

generics are now considered an advanced, powerful term in any language. When I first contacted the template in C + +, I had some doubts about it. Later, I read Bjarne Stroustrop's "The design and Evolution of C + +" to find that the use of templates is just as easy as the macros in C and the simple string substitution templates that are used to replace them. In fact, templates and generics are the same thing-although their implementations are slightly different.

C # generics support defines an algorithm and its data type at the point of use. In some earlier versions of C #, we can prove that no generics can work, because each type is derived from a common base type-object. This means that programmers can define a stack class based on the object type and put everything on that stack (since everything is derived from object). However, an object stack means that a customer object, an integer object, and an imaginary object can be placed on an instance of the same stack. As a result, developers want to subclass data types to bind data types to what they want to interact with. For example, when writing a custom business object, we recommend defining a strongly typed collection that is derived from System.Collections.CollectionBase. The reason is simple: Based on the object definition everything is considered to be a weak type definition.

Experts in the industry were convinced decades ago that strong types are better than weak types, so. NET finally supports the strong type, which seems to be a natural thing. Strongly typed algorithms of course recommend typing parameters-which is exactly what we use in generics.

For more than 10 of years, we have been using the letter T as the name of the typed parameter. In this way, you can find T in any type of data provided by the user of the generic class. The key to using generics is simply to provide this t. The key to defining generics is to implement a method or class and replace T with a specific data type.

Generics in C # support some other refinement. For example, a method or class can have multiple parameterized types and the C # generics also support a WHERE constraint-it is used to specify the type of the typed parameter. For example, if a generic type must implement an interface IDisposable, then the C # Generics support the implementation of this restriction. In the end of the article we also have to look at the constraint problem.
Less gossip, let's get to the other.

ii. using Generic collections

Some people ask me, "Where is the promise of object-oriented programming (OOP)?" "My answer is to look at OOP in two ways: the OOP you use and the OOP you create. If we take a quick look at the OO framework such as the VCL of Microsoft's. Net,borland, and all the Third-party components, many advanced applications are almost impossible to create. So, we can say that OOP has achieved its promise. Yes, it's difficult and potentially frustrating to produce good OOP code, but remember, you don't have to go through OOP to achieve your goals. So, let's take a look at the use of generics first.

When you create a project with a quick development tool such as Visual Studio or C # Express, you see a reference reference to the System.Collections.Generic namespace. In this namespace, there are several generic data structures-all of which support typed collections, hashes, queues, stacks, dictionaries, and lists. To use these powerful data structures, all you have to do is provide data types.

Listing 1 shows how easy it is to define a customer object with a strongly typed collection.

Listing 1 This console application contains a customer class and a strongly typed collection of customers based on list<t>.

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace generics{
Class program{
static void Main (string[] args) {
List<customer> customers = new list<customer> ();
Customers. ADD (New Customer ("Motown-jobs"));
Customers. ADD (New Customer ("Fatman ')");
foreach (Customer C in Customers)
Console.WriteLine (C.customername);
Console.ReadLine ();
}
}
public class customer{
private string customerName = "";
public string customername{
get {return customerName;}
set {customerName = value;}
}
Public Customer (String customerName) {
This.customername = CustomerName;
}
}
}


Note that we have a strongly typed set-list<customer>-that does not need to write a code for the collection class itself. If we want to extend the list of Customer, we can derive a new class by inheriting from List<customer>.

Total 3 page: previous 1 [2] [3] Next 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.