469,348 Members | 1,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,348 developers. It's quick & easy.

why "type inference " cann't work


I have a generic method in a class as follow:

T Get<T>(string key)
{
.....
return Convert.ChangeType(v, typeof(T))
}

when I use it in my code:

string ret = MyObject.Get("key");

compile failed , type inference cann't work, why?

thanks for any advice!

--

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Nie Longhai, Coder ^_*

Shanghai Xomi Instruments Co.,Ltd.

Phone: 086-021-57513966(807)
Fax: 086-021-57513966
Cell: 13162055440
Email: ni***@xomi.cn, co****@163.com
Web: www.xomi.cn
Zip: 201411

Shanghai C&U Industrial Park
Feng Cheng Town, Feng Xian District
Sep 27 '06 #1
4 1245
Nie longhai <co****@163.comwrote:
I have a generic method in a class as follow:

T Get<T>(string key)
{
....
return Convert.ChangeType(v, typeof(T))
}

when I use it in my code:

string ret = MyObject.Get("key");

compile failed , type inference cann't work, why?
Type inference is only performed based on parameters, not return
values.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 27 '06 #2

Hi, Jon

thank you for your help.
but

Why C# cann't infer type base on return type and cann't overload base on
return type ?
"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:MP************************@msnews.microsoft .com...
Nie longhai <co****@163.comwrote:
>I have a generic method in a class as follow:

T Get<T>(string key)
{
....
return Convert.ChangeType(v, typeof(T))
}

when I use it in my code:

string ret = MyObject.Get("key");

compile failed , type inference cann't work, why?

Type inference is only performed based on parameters, not return
values.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 27 '06 #3
Nie longhai wrote:
thank you for your help.
but

Why C# cann't infer type base on return type and cann't overload base on
return type ?
It was a choice made by the language designers to make things less
context-sensitive, so you can always tell from a method call which
overload will be taken etc without the compiler having to take into
account where the result will be used.

It can be a pain at times, but I think overall it adds to readability
of code and simplicity of the language specification.

Jon

Sep 27 '06 #4
Hi, Jon

thank you very much.

"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:11**********************@b28g2000cwb.google groups.com...
Nie longhai wrote:
>thank you for your help.
but

Why C# cann't infer type base on return type and cann't overload base on
return type ?

It was a choice made by the language designers to make things less
context-sensitive, so you can always tell from a method call which
overload will be taken etc without the compiler having to take into
account where the result will be used.

It can be a pain at times, but I think overall it adds to readability
of code and simplicity of the language specification.

Jon

Sep 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Kay | last post: by
3 posts views Thread by Wayne | last post: by
3 posts views Thread by Lord0 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.