[英]C# how to use generic type
我有一個使用這樣的泛型的類型
public class Stack<T> {
public void MyMethod() ...
}
在另一個類中,我想編寫一個對任何T都采用此Stack-type的方法:
public class MyClass {
public void MyMethod(Stack<T> stack) {
stack.MyMethod();
}
}
那可能嗎?
使MyClass
的類或方法通用。
通用類:
public class MyClass<T> {
public void MyMethod(Stack<T> stack) {
stack.MyMethod();
}
}
通用方法:
public class MyClass {
public void MyMethod<T>(Stack<T> stack) {
stack.MyMethod();
}
}
哪個合適取決於您希望T
可變的范圍。 如果MyClass
的單個實例應該能夠使用幾種不同類型的堆棧來調用MyMethod
,則該方法應該是通用的。 如果MyClass
的一個實例應要求對MyMethod
所有調用都傳遞相同種類的堆棧,則整個類應是通用的。
要么:
public class MyClass
{
public void MyMethod<T>(Stack<T> stack)
{
stack.MyMethod();
}
}
要么
public class MyClass<T>
{
public void MyMethod(Stack<T> stack)
{
stack.MyMethod();
}
}
是正確的。
有兩種類型的泛型:
一個=通用方法
兩個=通用類
例如 :
using System;
class Test<T>
{
T _value;
public Test(T t)
{
// The field has the same type as the parameter.
this._value = t;
}
public void Write()
{
Console.WriteLine(this._value);
}
}
class Program
{
static void Main()
{
// Use the generic type Test with an int type parameter.
Test<int> test1 = new Test<int>(5);
// Call the Write method.
test1.Write();
// Use the generic type Test with a string type parameter.
Test<string> test2 = new Test<string>("cat");
test2.Write();
}
}
您可以在通用類中設置constraints
。
例如
using System;
using System.Data;
/// <summary>
/// Requires type parameter that implements interface IEnumerable.
/// </summary>
class Ruby<T> where T : IDisposable
{
}
/// <summary>
/// Requires type parameter that is a struct.
/// </summary>
class Python<T> where T : struct
{
}
/// <summary>
/// Requires type parameter that is a reference type with a constructor.
/// </summary>
class Perl<V> where V : class, new()
{
}
class Program
{
static void Main()
{
// DataTable implements IDisposable so it can be used with Ruby.
Ruby<DataTable> ruby = new Ruby<DataTable>();
// Int is a struct (ValueType) so it can be used with Python.
Python<int> python = new Python<int>();
// Program is a class with a parameterless constructor (implicit)
// ... so it can be used with Perl.
Perl<Program> perl = new Perl<Program>();
}
}
而對於通用方法
using System;
using System.Collections.Generic;
class Program
{
static List<T> GetInitializedList<T>(T value, int count)
{
// This generic method returns a List with ten elements initialized.
// ... It uses a type parameter.
// ... It uses the "open type" T.
List<T> list = new List<T>();
for (int i = 0; i < count; i++)
{
list.Add(value);
}
return list;
}
static void Main()
{
// Use the generic method.
// ... Specifying the type parameter is optional here.
// ... Then print the results.
List<bool> list1 = GetInitializedList(true, 5);
List<string> list2 = GetInitializedList<string>("Perls", 3);
foreach (bool value in list1)
{
Console.WriteLine(value);
}
foreach (string value in list2)
{
Console.WriteLine(value);
}
}
}
是的,可以,但是您必須“通知”正在使用的其他類型,例如:
public class StackType<t>
{
public void Generate()
{
}
}
public class MyClass<T>
{
public MyClass(StackType<T> stack)
{
stack.Generate();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.