By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

why "type inference " cann't work

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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.