简体   繁体   English

ToArray()和ToArray之间的区别 <int> ();

[英]Difference between ToArray() & ToArray<int>();

Excuse me if I am asking silly question, but can anybody explain the difference between following two calls ( ToArray ). 对不起,如果我问愚蠢的问题,但任何人都可以解释下面两个电话之间的区别( ToArray )。 In the intellisense it does not show them as overloaded methods & of course the output of both the calls are same. 在intellisense中它不会将它们显示为重载方法,当然两个调用的输出都是相同的。

List<int> i = new List<int> { 1, 2, 5, 64 };
int[] input = i.Where(j => j % 2 == 1).ToArray();
input = i.Where(j => j % 2 == 1).ToArray<int>();

There is no difference, it is the exact same ToArray() method. 没有区别,它与ToArray()方法完全相同。 The compiler can simply infer that you want the ToArray<int> version from the syntax of the expression. 编译器可以简单地从表达式的语法推断出你想要ToArray<int>版本。 The return value of Where() was inferred to return int. 推断Where()的返回值返回int。 In other words, it uses Where<int>() . 换句话说,它使用Where<int>() Which was inferred from the type of the List<>. 这是从List <>的类型推断出来的。 So it can infer that you need ToArray<int> . 所以它可以推断你需要ToArray<int>

So the type inference chain is List<int> => Where<int>() => ToArray<int>() . 所以类型推断链是List<int> => Where<int>() => ToArray<int>()

Change the list to, say, List<long> and the expression still works without having to modify it. 将列表更改为List<long> ,表达式仍然有效,无需修改。

There is no difference here. 这里没有区别。 In the first call, the compiler has inferred the type int , while in the second you have specified it explicitly. 在第一次调用中,编译器推断出int类型,而在第二次调用中,您已明确指定它。

There may be cases where the type is necessary because it cannot be inferred. 可能存在类型是必要的情况,因为无法推断。 For example, you have a custom collection that implements IEnumerable<T> twice, for two different types T . 例如,对于两种不同类型的T ,您有一个自定义集合实现IEnumerable<T>两次。 This hurts usability so it is preferable that you avoid such constructions. 这会损害可用性,因此最好避免使用这种结构。

It's the same generic method. 它是相同的通用方法。 In the first case, the generic type parameter is inferred by compiler from the generic type parameter of the Enumeration you're calling ToArray<T>() on. 在第一种情况下,泛型类型参数由编译器根据您调用ToArray<T>()的枚举的泛型类型参数推断。 But you can also specify it explicitly. 但您也可以明确指定它。

They are the same thing. 他们是一样的东西。 You are witnessing what is called "type inference". 您正在目睹所谓的“类型推断”。 In certain scenarios, the C# compiler can detect the type based on the parameters passed in and you do not have to explicitly specify the type parameters. 在某些情况下,C#编译器可以根据传入的参数检测类型,您不必显式指定类型参数。 In your example, it's known that i is an IEnumerable<int> , and thus .ToArray() can infer the parameter int . 在您的示例中,已知iIEnumerable<int> ,因此.ToArray()可以推断出参数int

Here is a nice article that goes into this in depth: http://joelabrahamsson.com/a-neat-little-type-inference-trick-with-c/ 这篇文章深入探讨了这篇文章: http//joelabrahamsson.com/a-neat-little-type-in​​ference-trick-with-c/

No difference. 没有不同。

int[] input = i.Where(j => j % 2 == 1).ToArray();

Here, only the compiler is inferring the T generic argument based on the type of enumerable you call ToArray() on. 这里,只有编译器根据您调用ToArray()的可枚举类型推断T generic参数。

input = i.Where(j => j % 2 == 1).ToArray<int>();

Here, return value of Where() is inferred by compiler to return int . 这里, Where()返回值由编译器推断为返回int

  • ToArray<T>() is Generic is so that it can operate on any IEnumerable<T> . ToArray<T>()是Generic,因此它可以在任何IEnumerable<T>
  • ToArray() actually just redirects to an implicit interpretation of ToArray<T>() based on the T of the source IEnumerable. ToArray()实际上只是根据源IEnumerable的T重定向到ToArray<T>()的隐式解释。
  • If you call a generic method and don't supply any type arguments, the compiler will try to infer them for you. 如果您调用泛型方法并且不提供任何类型参数,编译器将尝试为您推断它们。

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

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