简体   繁体   English

通用列表的扩展方法

[英]Extension method on generic list

I'm quite new to C#, and have two questions regarding generic lists and extension methods. 我对C#相当陌生,对于泛型列表和扩展方法有两个问题。 Sorry if the questions are a bit stupid.. 对不起,如果问题有点愚蠢。

What is the difference between: 之间有什么区别?

public static IObjectWithTempID FindByTempID
    (this ObservableCollection<IObjectWithTempID > list, long tempID) 

and

public static IObjectWithTempID FindByTempID< E >
   (this ObservableCollection< IObjectWithTempID > list, long tempID)

I have tried to read up on the subject, but still don't understand :$ 我已经尝试阅读有关该主题的内容,但仍然不了解:$

I have run into a strange issue. 我遇到了一个奇怪的问题。 When I declare an ObservableCollection , such as this 当我声明一个ObservableCollection ,例如这样

ObservableCollection<TestObjectWithTempID> a =
    new ObservableCollection<TestObjectWithTempID>();

it is possible to call extension method 可以调用扩展方法

public static long FindByTempID
    (this IEnumerable< IObjectWithTempID > list, long tempID){}

on the list. 在清单上。 It is surprisingly not possible to call extension method 令人惊讶的是不可能调用扩展方法

public static long FindByTempID
    (this ObservableCollection< IObjectWithTempI D> list, long tempID){} 

though. 虽然。 What have I missed here? 我在这里错过了什么?

For 2, it needs to be: 对于2,它必须为:

TheMethod<T>(ObservableCollection<T> list)
    where T : ISomeInterface

Otherwise you could attempt to add any ISomeInterface instance to a more-specific list. 否则,您可以尝试将任何 ISomeInterface实例添加到更特定的列表中。

For your first question: 对于第一个问题:

In the second method declaration the E in FindByTempID<E> is a type parameter . 在第二个方法声明中, FindByTempID<E>中的FindByTempID<E>类型参数 In other words it is a placeholder for a type, not an actual type. 换句话说,它是类型的占位符 ,而不是实际类型。 E can be used in the method body as if it were a real type. E可以在方法主体中使用,就像它是实类型一样。 The general term for this type of programming is generics . 这类编程的总称是泛型

Having these extension methods 具有这些扩展方法

public static class Extenders
{
    public static void FindByTempID(this ObservableCollection<IObjectWithTempID> e, long tempID)
    {
    }
    public static void FindByTempID2(this IEnumerable<IObjectWithTempID> e, long tempID)
    {
    }
    public static void FindByTempID3(this ObservableCollection<TestObjectWithTempID> e, long tempID)
    {
    }
    public static void FindByTempID4<T>(this ObservableCollection<T> e, long tempID)
    {
    }
}

Then 然后

var a1 = new ObservableCollection<TestObjectWithTempID>();
var a2 = new ObservableCollection<IObjectWithTempID>();

//a1.FindByTempID(1); /* Is not a collection of interface */
a1.FindByTempID2(1); // IEnumerable<T> is a generic list
a1.FindByTempID3(1); // It is a collection of TestObjectWithTempID
a1.FindByTempID4(1); // Identify the collection as ObservableCollection<TestObjectWithTempID>

a2.FindByTempID(1); // Is a collection of interface
a2.FindByTempID2(1); // IEnumerable<T> is a generic list
//a2.FindByTempID3(1); /* Is not a collection of TestObjectWithTempID */
a2.FindByTempID4(1); // Identify the collection as ObservableCollection<IObjectWithTempID>

IEnumerable<T> 的IEnumerable <T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM