Can the generic type be used before defination?泛型可以在定义前使用吗?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
【中文问题版本】
.NET中泛型通常遵循â先定义,后使用â原则ââ比如系统预定义的Func带有返回值的委托,你必须:
Func<int,int> j=(i)=>i;
而不能:
Func<int,?> j=(i)=>i; //因为这里会报告T没有定义âââ这里?表示空缺,是不允许的â。
但是我注意到几乎所有的扩展函数(比如Select函数)ââ
int[]numbers=ââ//省略定义
numbers.Select(i=>i); 注意,Select里边需要一个Func<TSource,TResult>,但是此处TResult在上面例子中貌似没有定义啊?你没有告诉它是int还是什么类型。
难道说:
1)在扩展函数情况下,允许省略定义直接使用,NET的CLR会直接感知出来?
2)此类情况还作用于哪些情况下?
3)既然Select是需要一个Func<TSource,TResult>的委托,那么我可以显示写成Select<类型1,类型2>(i=>i);来调用吗?那么1和3的情况的差别何在啊?
<pre style="background:white; color:black; font-family:NSimSun <span style="color:blue int[] <span>numbers <span>= { <span>1,<span>2,<span>3,<span>4,<span>5};<br/><span>numbers<span>.<span>Select<span><<span style="color:blue int, <span style="color:blue string<span>>(<span>i <span>=> <span>i<span>.<span>ToString());[/code]
【English Version】
In .NET framework, You should define the specific generic to tell which type(s) you are going to use before they are usedââLets take the .NETs pre-defined functionââFunc for an example:
Func<int,int> j=(i)=>i;
But you cannot sayââ
Func<int,?> j=(i)=>i; //No defination for ?, so this is invalid, which will cause a syntax error.
But Ive noticed nearly all the extended method (since net framework 3.5)ââ
int[]numbers=ââ//omit defination hereââ
numbers.Select(i=>i); //"Select" needs a delegate of Func<TSource,TResult>,but wheres the defination for "TResult" in the above sample?You havent told it whether its of int or some other type?
Can I think:
1)In the extended method,we can use it before defination and NETsCLR will know that type?
2)Where can I also see such situation of "intellisensely-telling type for a generic type"?
3)Since "Select" is a delegate that needs Func<TSource,TResult>,I can also define exclipitly it as "Select<type1,type2>(i=>i);"
<br/>
Can I use like that?Whats the difference between 1 and 3?Sample looks like below:
<pre style="background:white; color:black; font-family:NSimSun <span style="color:blue int[] <span>numbers <span>= { <span>1,<span>2,<span>3,<span>4,<span>5};<br/><span>numbers<span>.<span>Select<span><<span style="color:blue int, <span style="color:blue string<span>>(<span>i <span>=> <span>i<span>.<span>ToString());[/code]
<
http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=-JGdkJmYk5Kbo4yOlYqdiJm8jY3Sn5OR" target="_blank <img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png" alt=" QQ我: http://sighttp.qq.com/authd?IDKEY=c5a1e2c9ea4f0b16c65453c0116275d7eded425641624137" target="_blank <img title="讨论(Talk)" src="http://wpa.qq.com/imgd?IDKEY=c5a1e2c9ea4f0b16c65453c0116275d7eded425641624137&pic=46 &r=0.7423044074346896" border="0" alt="讨论(Talk)
<br/>
http://msdn.microsoft.com/zh-cn/gg650455.aspx?ocid=ban-f-cn-wag-20110225-MSDN&ocid=soc-f-cn-win--SINA 下载MSDN桌面工具(Vista,Win7)
<br/>
http://www.cnblogs.com/ServiceboyNew/ 我的博客园 <br/>
http://www.bushinet.com/dj/ 慈善点击,点击此处 <br/>

View the full article
 
Back
Top