在 C# 中使用 List<T>(泛型)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19138991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:13:10  来源:igfitidea点击:

Using List<T> in C# (Generics)

c#generics

提问by makoshichi

That's a pretty elementary question, but I have never delved into generics before and I found myself in the need to use it. Unfortunately I don't have the time right now to go through any tutorials and the answers I found to related questions so far aren't what one could call basic, so there we go:

这是一个非常基本的问题,但我以前从未深入研究过泛型,我发现自己需要使用它。不幸的是,我现在没有时间阅读任何教程,到目前为止我找到的相关问题的答案并不是人们可以称之为基本的答案,所以我们走了:

Let's say I have the following:

假设我有以下内容:

List<MyClass1> list1 = getListType1();
List<MyClass2> list2 = getListType2();

if (someCondition)
    MyMethod(list1);
else
    MyMethod(list2);

And of course

而且当然

void MyMethod(List<T> list){
    //Do stuff
}

Well, I thought it would be this simple, but apparently it is not. VS warns me that

好吧,我以为会这么简单,但显然不是。VS 警告我

The type arguments for method MyMethod(System.Collections.Generic.List) cannot be inferred from the usage

无法从用法推断方法 MyMethod(System.Collections.Generic.List) 的类型参数

and if I compile it anyway, I get a

如果我无论如何编译它,我会得到一个

The type or namespace name 'T' could not be found

找不到类型或命名空间名称“T”

error.

错误。

In the many answers I found, I read that I have to declare what Tis, which makes sense, but I couldn't quite grasp how to do so in such a simplistic scenario. Of course, those answers created even more questions in my mind, but right now I just want an explanation of what I'm doing wrong (besides not studying generics) and how to make it right.

在我找到的许多答案中,我读到我必须声明是什么T,这是有道理的,但在如此简单的情况下,我无法完全掌握如何做到这一点。当然,这些答案在我的脑海中产生了更多的问题,但现在我只想解释我做错了什么(除了不学习泛型)以及如何使它正确。

采纳答案by mattytommo

You need to declare Tagainst the method, then C# can identify the type the method is receiving. Try this:

您需要T针对该方法进行声明,然后 C# 才能识别该方法正在接收的类型。尝试这个:

void MyMethod<T>(List<T> list){
    //Do stuff
}

Then call it by doing:

然后通过执行以下操作调用它:

if (someCondition)
    MyMethod(list1);
else
    MyMethod(list2);

You can make it even stricter, if all classes you are going to pass to the method share a common base class:

如果要传递给该方法的所有类共享一个公共基类,则可以使其更严格:

void MyMethod<T>(List<T> list) where T : MyClassBase

回答by Royi Mindel

You need to let c# know what type is sent:

您需要让 c# 知道发送的是什么类型:

List<MyClass1> list1 = getListType1();
List<MyClass2> list2 = getListType2();

if (someCondition)
    MyMethod<MyClass1>(list1);
else
    MyMethod<MyClass2>(list2);

void MyMethod<T>(List<T> list){
    //Do stuff
}

回答by Chris Mantle

You need to add the generic type parameter for Tto your method:

您需要为T您的方法添加泛型类型参数:

void MyMethod<T>(List<T> list) {

The compiler doesn't know what Trepresents, otherwise.

编译器不知道T代表什么,否则。

回答by Pradeep Yadav

Basically, In C#, List < T >class represents a strongly typed list of objects that can be accessed by index.

基本上,在 C# 中,List < T >类表示可以通过索引访问的强类型对象列表。

And it also supports storing values of a specific type without casting to or from object.

并且它还支持存储特定类型的值,而无需向对象或从对象进行转换。

we can use in Interger value & String Value in the List.

我们可以在 List 中的 Interger value & String Value 中使用。