C # 2.0 generic class creation and use

Source: Internet
Author: User
Tags data structures

"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 frameworks like Microsoft's. NET, Borland, and all of 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's"));
   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>-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>.

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.