什么是隐含的泛型类型参数 [英] What are implied generic type parameters

查看:89
本文介绍了什么是隐含的泛型类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我碰到一个答案刊登了Servy( http://stackoverflow.com/a/15098242/496680 )和他的一些代码做这样的:

 公共静态INT二分查找< TSource,TKEY的>(...)

有关扩展方法,但他称这样的:

  arr.BinarySearch(...)

我一打听,有人说文件档案的一个隐含的泛型类型参数。
我GOOGLE了他们,但发现没有对他们的信息。
我理解泛型是如何工作的,但我没能理解如何/时使用它们。




  1. 为什么servy使用它们在他的延伸的方法?

  2. 是否有这些,我可以搜索?


解决方案

好吧,你离开了,使得它所有的工作中最重要的组成部分。该类型参数可以通过传入的实际对象的参数来推断



例如:

 静态类扩展{
内部静态的IEnumerable< U>测试< T,U>(
本的IEnumerable< T>的项目,
Func键< T,U>变频器){
的foreach(在项目牛逼项){
收益率的回报转换器(项目);
}
}
}

这扩展方法适用于任何的IEnumerable的类,并且将每个项目枚举转换为根据您所提供的转换另一种类型。这是标准的仿制药



现在,有很多方法来调用这个方法:

 的IEnumerable< INT>值= Enumerable.Range&所述; INT>(1,10); 
Func键< INT,串>换算= I => i.ToString(0.00);

//变化1,明确调用
IEnumerable的<串GT;结果1 = Extensions.Test< INT,串>(值,转换器);

//变化2,明确的调用进行类型推理
IEnumerable的<串GT;结果2 = Extensions.Test(值,转换器);

//变异3,扩展方法调用,还提供明确的类型
IEnumerable的<串GT;结果3 = values.Test< INT,串>(转换器);

//变化4,扩展方法与类型推断
IEnumerable的<串GT; results4 = values.Test(转换器);



所有四种变化调用相同的方法,并返回相同的结果。类型推断的工作原理是在寻找传递的参数,并根据所提供什么自动推断其类型。在上面的例子中,它能够确定键入 T 的类型为 INT ,因为我们在通过了的IEnumerable< INT> 的IEnumerable< T> 。它也能够推断出该类型 U 的类型为字符串,因为我们在一个函数功能通过匹配初始型 T INT 并返回一个字符串。因此, Func键< T,U> 与LT 我们的转换函数func&填写;整型,字符串方式>



从上面的推理,它是在该点一个标准的通用方法。类型推断和扩展方法无非是便利/语法糖多。事实上,如果你反编译的输出,你可以看到,扩展方法被替换为静态调用,通常与类型参数明确填写定义。 (这个变化的基础上你的反编译器和设置选项)。


So, I ran across an answer by Servy ( http://stackoverflow.com/a/15098242/496680 ) and some of his code does this:

public static int BinarySearch<TSource, TKey>(...)

for an extension method, but he calls it like this:

arr.BinarySearch(...)

I asked around and somebody metioned that its an implied generic type parameter. I googled them but found no information on them. I understand how generics work but I'm failing to understand how/when to use these.

  1. Why does servy use them in his extention method?
  2. Is there a more official name for these that I can search for?

解决方案

Well, you left out the most important part that makes it all work. The type parameters can be inferred by the actual object parameters passed in.

For instance:

static class Extensions {
  internal static IEnumerable<U> Test<T, U>(
                                   this IEnumerable<T> items,
                                   Func<T, U> converter) {
    foreach (T item in items) {
      yield return converter(item);
    }
  }
}

This extension method works on any IEnumerable class and will convert each item in the enumeration to another type based on the converter you provided. This is standard generics.

Now, there are many ways to call this method:

IEnumerable<int> values = Enumerable.Range<int>(1, 10);
Func<int, string> converter = i => i.ToString("0.00");

// Variation 1, explicit calling
IEnumerable<string> results1 = Extensions.Test<int, string>(values, converter);

// Variation 2, explicit calling with type inference
IEnumerable<string> results2 = Extensions.Test(values, converter);

// Variation 3, extension method calling, still providing explicit types
IEnumerable<string> results3 = values.Test<int, string>(converter);

// Variation 4, extension method with type inference
IEnumerable<string> results4 = values.Test(converter);

All four variations call the same method and return the same result. Type inference works by looking at the parameters passed and automatically inferring their types based on what's being provided. In our examples above, it's able to determine that type T is of type int because we passed in an IEnumerable<int> into the parameter for IEnumerable<T>. It is also able to infer that type U is of type string because we passed in a Func matching the initial type of T with int and returning a string. So the Func<T, U> is filled in with our converter function of Func<int, string>.

From the inference above, it's a standard generic method at that point. Type inference and extension methods are nothing more than convenience/syntactic sugar. In fact, if you decompile the output you can see that extension methods are replaced with static calls and are usually defined with the type parameters explicitly filled out. (This varies based on your decompiler and the set options).

这篇关于什么是隐含的泛型类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆