กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
แอปอัจฉริยะ เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้เบราว์เซอร์นี้ไม่ได้รับการสนับสนุนอีกต่อไป
อัปเกรดเป็น Microsoft Edge เพื่อใช้ประโยชน์จากคุณลักษณะล่าสุด เช่น การอัปเดตความปลอดภัยและการสนับสนุนด้านเทคนิค
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 parameter T
, 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 replaces T
with int
. Generics are most frequently used with collections and the methods that operate on them. The System.Collections.Generic namespace contains several generic-based collection classes. The nongeneric collections, such as ArrayList aren't recommended and are maintained only for compatibility purposes. For more information, see Generics 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 the List<T> class provided by .NET instead of creating your own.) The type parameter T
is used in several locations where a concrete type would ordinarily be used to indicate the type of the item stored in the list:
AddHead
method.Data
property in the nested Node
class.data
in the nested class.T
is available to the nested Node
class. When GenericList<T>
is instantiated with a concrete type, for example as a GenericList<int>
, each occurrence of T
is replaced with int
.
// 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 generic GenericList<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");
หมายเหตุ
Generic types aren't limited to classes. The preceding examples use class
types, but you can define generic interface
and struct
types, including record
types.
For more information, see the C# Language Specification.
คำติชม .NET
.NET เป็นโครงการโอเพนซอร์ส เลือกลิงก์เพื่อให้คำติชม:
กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
แอปอัจฉริยะ เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้