Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Generic classes and methods

  • 2024-03-19
Feedback

In this article

Generics introduces the concept of type parameters to .NET. Generics make it possible to design classes and methods that defer the specification of one or more type parameters until you use the class or method in your code. For example, by using a generic type parameterT, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here:

// Declare the generic class.public class GenericList<T>{    public void Add(T item) { }}public class ExampleClass { }class TestGenericList{    static void Main()    {        // Create a list of type int.        GenericList<int> list1 = new();        list1.Add(1);        // Create a list of type string.        GenericList<string> list2 = new();        list2.Add("");        // Create a list of type ExampleClass.        GenericList<ExampleClass> list3 = new();        list3.Add(new ExampleClass());    }}

Generic classes and methods combine reusability, type safety, and efficiency in a way that their nongeneric counterparts can't. Generic type parameters are replaced with the type arguments during compilation. In the preceding example, the compiler replacesT withint. Generics are most frequently used with collections and the methods that operate on them. TheSystem.Collections.Generic namespace contains several generic-based collection classes. The nongeneric collections, such asArrayList aren't recommended and are maintained only for compatibility purposes. For more information, seeGenerics in .NET.

You can also create custom generic types and methods to provide your own generalized solutions and design patterns that are type-safe and efficient. The following code example shows a simple generic linked-list class for demonstration purposes. (In most cases, you should use theList<T> class provided by .NET instead of creating your own.) The type parameterT is used in several locations where a concrete type would ordinarily be used to indicate the type of the item stored in the list:

  • As the type of a method parameter in theAddHead method.
  • As the return type of theData property in the nestedNode class.
  • As the type of the private memberdata in the nested class.

T is available to the nestedNode class. WhenGenericList<T> is instantiated with a concrete type, for example as aGenericList<int>, each occurrence ofT is replaced withint.

// Type parameter T in angle brackets.public class GenericList<T>{    // The nested class is also generic, and    // holds a data item of type T.    private class Node(T t)    {        // T as property type.        public T Data { get; set; } = t;        public Node? Next { get; set; }    }    // First item in the linked list    private Node? head;    // T as parameter type.    public void AddHead(T t)    {        Node n = new(t);        n.Next = head;        head = n;    }    // T in method return type.    public IEnumerator<T> GetEnumerator()    {        Node? current = head;        while (current is not null)        {            yield return current.Data;            current = current.Next;        }    }}

The following code example shows how client code uses the genericGenericList<T> class to create a list of integers. If you change the type argument, the following code creates lists of strings or any other custom type:

// A generic list of int.GenericList<int> list = new();// Add ten int values.for (int x = 0; x < 10; x++){    list.AddHead(x);}// Write them to the console.foreach (int i in list){    Console.WriteLine(i);}Console.WriteLine("Done");

Note

Generic types aren't limited to classes. The preceding examples useclass types, but you can define genericinterface andstruct types, includingrecord types.

Generics overview

  • Use generic types to maximize code reuse, type safety, and performance.
  • The most common use of generics is to create collection classes.
  • The .NET class library contains several generic collection classes in theSystem.Collections.Generic namespace. The generic collections should be used whenever possible instead of classes such asArrayList in theSystem.Collections namespace.
  • You can create your own generic interfaces, classes, methods, events, and delegates.
  • Generic classes can be constrained to enable access to methods on particular data types.
  • You can obtain information at run time on the types that are used in a generic data type by using reflection.

C# language specification

For more information, see theC# Language Specification.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo