C#如何使用泛型类型
我有一个类型使用这样的泛型C#如何使用泛型类型,c#,generics,C#,Generics,我有一个类型使用这样的泛型 public class Stack<T> { public void MyMethod() ... } 公共类堆栈{ public void MyMethod()。。。 } 在另一个类中,我想编写一个方法,该方法对任何T都采用此堆栈类型: public class MyClass { public void MyMethod(Stack<T> stack) { stack.MyMethod();
public class Stack<T> {
public void MyMethod() ...
}
公共类堆栈{
public void MyMethod()。。。
}
在另一个类中,我想编写一个方法,该方法对任何T都采用此堆栈类型:
public class MyClass {
public void MyMethod(Stack<T> stack) {
stack.MyMethod();
}
}
公共类MyClass{
公共void MyMethod(堆栈){
stack.MyMethod();
}
}
这可能吗?在
MyClass
中创建类或方法
通用类:
public class MyClass<T> {
public void MyMethod(Stack<T> stack) {
stack.MyMethod();
}
}
公共类MyClass{
公共void MyMethod(堆栈){
stack.MyMethod();
}
}
一般方法:
public class MyClass {
public void MyMethod<T>(Stack<T> stack) {
stack.MyMethod();
}
}
公共类MyClass{
公共void MyMethod(堆栈){
stack.MyMethod();
}
}
这取决于希望
T
可变的范围。如果MyClass
的单个实例应该能够使用几种不同类型的堆栈调用MyMethod
,那么该方法应该是泛型的。如果MyClass
的单个实例需要对MyMethod
的所有调用来传递相同类型的堆栈,那么整个类应该是泛型的。在MyClass
中创建类或方法
通用类:
public class MyClass<T> {
public void MyMethod(Stack<T> stack) {
stack.MyMethod();
}
}
公共类MyClass{
公共void MyMethod(堆栈){
stack.MyMethod();
}
}
一般方法:
public class MyClass {
public void MyMethod<T>(Stack<T> stack) {
stack.MyMethod();
}
}
公共类MyClass{
公共void MyMethod(堆栈){
stack.MyMethod();
}
}
这取决于希望T
可变的范围。如果MyClass
的单个实例应该能够使用几种不同类型的堆栈调用MyMethod
,那么该方法应该是泛型的。如果MyClass
的单个实例需要对MyMethod
的所有调用来传递相同类型的堆栈,那么整个类应该是泛型的。或者:
public class MyClass
{
public void MyMethod<T>(Stack<T> stack)
{
stack.MyMethod();
}
}
公共类MyClass
{
公共void MyMethod(堆栈)
{
stack.MyMethod();
}
}
或
公共类MyClass
{
公共void MyMethod(堆栈)
{
stack.MyMethod();
}
}
是正确的。以下任一项:
public class MyClass
{
public void MyMethod<T>(Stack<T> stack)
{
stack.MyMethod();
}
}
公共类MyClass
{
公共void MyMethod(堆栈)
{
stack.MyMethod();
}
}
或
公共类MyClass
{
公共void MyMethod(堆栈)
{
stack.MyMethod();
}
}
是正确的。是的,您可以,但您必须“通知”您正在使用的其他类,如:
public class StackType<t>
{
public void Generate()
{
}
}
public class MyClass<T>
{
public MyClass(StackType<T> stack)
{
stack.Generate();
}
}
公共类堆栈类型
{
public void Generate()
{
}
}
公共类MyClass
{
公共MyClass(StackType堆栈)
{
stack.Generate();
}
}
是的,您可以,但您必须“通知”其他类您正在使用的类型,如:
public class StackType<t>
{
public void Generate()
{
}
}
public class MyClass<T>
{
public MyClass(StackType<T> stack)
{
stack.Generate();
}
}
公共类堆栈类型
{
public void Generate()
{
}
}
公共类MyClass
{
公共MyClass(StackType堆栈)
{
stack.Generate();
}
}
有两种类型的泛型:
一=通用方法
二=泛型类
例如:
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();
}
}
对于一般方法
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);
}
}
}
使用系统;
使用System.Collections.Generic;
班级计划
{
静态列表GetInitializedList(T值,int计数)
{
//此泛型方法返回一个初始化了十个元素的列表。
//…它使用类型参数。
//…它使用“开放式”T。
列表=新列表();
for(int i=0;i
我的答案来源是这些链接 有两种类型的泛型: 一=通用方法 二=泛型类 例如:
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();
}
}
对于一般方法
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);
}
}
}
使用系统;
使用System.Collections.Generic;
班级计划
{
静态列表GetInitializedList(T值,int计数)
{
//此泛型方法返回一个初始化了十个元素的列表。
//…它使用类型参数。
//…它使用“开放式”T。
列表=新列表();
for(int i=0;i
我的答案来源是这些链接