简体   繁体   English

没有指定类型的C#通用方法

[英]C# Generic Method Without Specifying Type

Ok so I'm a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about how generics should work so I'm hoping someone can explain this to me: 好吧,我是一个开始使用C#的Java人员,我正在编写并开始制作一个通用方法,我编写的运行和编译,但它违背了我所知道的关于泛型应该如何工作的一切,所以我希望有人可以解释这个对我来说:

So I have a generic method defined as follows: 所以我有一个通用的方法定义如下:

public static List<T> CopyAsList<T>(IEnumerable<T> list, Object lockObject)  
{  
    if (list != null)  
    {  
        lock (lockObject)  
        {  
            return new List<T>(list);  
        }  
    }  
    return null;  
}  

But the weird thing to me is that I can call this generic method without ever specifying T and it will work: 但对我来说奇怪的是,我可以在不指定T情况下调用此泛型方法,它将起作用:

List<String> strings = new List<string>() { "a", "b", "c"};
List<int> ints = new List<int>() { 1,2,3};
object lockObject = new object();

foreach (string s in CopyAsList(strings, lockObject))
{
    Console.WriteLine(s);
}

foreach (int i in CopyAsList(ints, lockObject))
{
    Console.WriteLine(i);
}

How is it the code is able to compile without ever specifying the generic type? 如何在没有指定泛型类型的情况下编译代码? Does C# infer the type at runtime? C#是否在运行时推断出类型?

No, it is inferred at compile time - the generic type parameter in the IEnumerable<T> you supply is used, which is known at compile time. 不,它是在编译时推断的 - 使用了您提供的IEnumerable<T>的泛型类型参数,这在编译时是已知的。 Generally put, everything concerning generics and type parameters is specified at compile time. 通常,在编译时指定有关泛型和类型参数的所有内容 If there is mismatch of any kind, the compiler will complain and your code won't compile. 如果存在任何类型的不匹配,编译器抱怨并且您的代码将无法编译。

There are edge cases where you have to specify the types explicitly, these only occurs in rare circumstances with overloaded methods, sometimes with multiple combinations of type parameters. 有些边缘情况需要明确指定类型,这些仅在极少数情况下使用重载方法,有时会出现多种类型参数组合。

C# has a great many more compile-time and runtime type inference features than Java. 与Java相比,C#具有更多的编译时和运行时类型推断功能。 If this subject interests you, see my articles on the subject: 如果此主题感兴趣,请参阅我关于此主题的文章:

http://blogs.msdn.com/b/ericlippert/archive/tags/type+inference/ http://blogs.msdn.com/b/ericlippert/archive/tags/type+inference/

If you are in particular interested in generic method type inference and you have half an hour to spare, here's me explaining how we changed the type inference algorithm in C# 3: 如果您对泛型方法类型推断特别感兴趣,并且您有半小时的空闲时间,请在此解释我们如何更改C#3中的类型推断算法:

http://blogs.msdn.com/b/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx http://blogs.msdn.com/b/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx

The C# compiler can often infer the generic type at compile time . C#编译器通常可以在编译时推断泛型类型。 When it can do this, you do not need to specify the type for a generic method. 当它可以执行此操作时,您不需要指定泛型方法的类型。

This is a major part of what makes LINQ "usable". 这是使LINQ“可用”的主要部分。 Without compile time type inference, queries would look like: 如果没有编译时类型推断,查询将如下所示:

IEnumerable<int> myIds = myCollection
                             .Where<MyType>(i => i.Name == "Foo")
                             .Select<MyType, int>(i => i.Id);

Instead of being able to write: 而不是写:

var myIds = myCollection.Where(i => i.Name == "Foo").Select(i => i.Id);

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

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