Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
62 captures
15 Sep 2008 - 17 Sep 2024
JulSEPOct
Previous capture05Next capture
201020112012
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20110905155242/http://msdn.microsoft.com:80/en-us/library/sz6zd40f.aspx
Separator
Community Content
Avatar
  • Add code samples and tips to enhance this topic.
Separator
Advertisement
ExpandMinimize
MSDN

Generic Classes (C# Programming Guide)

Visual Studio 2010

Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on. Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored.

For most scenarios that require collection classes, the recommended approach is to use the ones provided in the .NET Framework class library. For more information about using these classes, seeGenerics in the .NET Framework Class Library (C# Programming Guide).

Typically, you create generic classes by starting with an existing concrete class, and changing types into type parameters one at a time until you reach the optimal balance of generalization and usability. When creating your own generic classes, important considerations include the following:

  • Which types to generalize into type parameters.

    As a rule, the more types you can parameterize, the more flexible and reusable your code becomes. However, too much generalization can create code that is difficult for other developers to read or understand.

  • What constraints, if any, to apply to the type parameters (SeeConstraints on Type Parameters (C# Programming Guide)).

    A good rule is to apply the maximum constraints possible that will still let you handle the types you must handle. For example, if you know that your generic class is intended for use only with reference types, apply the class constraint. That will prevent unintended use of your class with value types, and will enable you to use theas operator onT, and check for null values.

  • Whether to factor generic behavior into base classes and subclasses.

    Because generic classes can serve as base classes, the same design considerations apply here as with non-generic classes. See the rules about inheriting from generic base classes later in this topic.

  • Whether to implement one or more generic interfaces.

    For example, if you are designing a class that will be used to create items in a generics-based collection, you may have to implement an interface such asIComparable(OfT) whereT is the type of your class.

For an example of a simple generic class, seeIntroduction to Generics (C# Programming Guide).

The rules for type parameters and constraints have several implications for generic class behavior, especially regarding inheritance and member accessibility. Before proceeding, you should understand some terms. For a generic classNode<T>, client code can reference the class either by specifying a type argument, to create a closed constructed type (Node<int>). Alternatively, it can leave the type parameter unspecified, for example when you specify a generic base class, to create an open constructed type (Node<T>). Generic classes can inherit from concrete, closed constructed, or open constructed base classes:

No code example is currently available or this language may not be supported.

Non-generic, in other words, concrete, classes can inherit from closed constructed base classes, but not from open constructed classes or from type parameters because there is no way at run time for client code to supply the type argument required to instantiate the base class.

No code example is currently available or this language may not be supported.

Generic classes that inherit from open constructed types must supply type arguments for any base class type parameters that are not shared by the inheriting class, as demonstrated in the following code:

No code example is currently available or this language may not be supported.

Generic classes that inherit from open constructed types must specify constraints that are a superset of, or imply, the constraints on the base type:

No code example is currently available or this language may not be supported.

Generic types can use multiple type parameters and constraints, as follows:

No code example is currently available or this language may not be supported.

Open constructed and closed constructed types can be used as method parameters:

No code example is currently available or this language may not be supported.

If a generic class implements an interface, all instances of that class can be cast to that interface.

Generic classes are invariant. In other words, if an input parameter specifies aList<BaseClass>, you will get a compile-time error if you try to provide aList<DerivedClass>.

Community ContentAdd
Advertisement
© 2011 Microsoft. All rights reserved.
Terms of Use |Trademarks |Privacy Statement|Feedback FeedbackFeedback
Feedback
Tell us about your experience...
Did the page load quickly?
Yes No
Do you like the page design?
Yes No
How useful is this topic?
Tell us more

[8]ページ先頭

©2009-2025 Movatter.jp