473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript">...
1
by: Mark Sandfox | last post by:
Is there a way to restrict the user to only selecting and sending either a ..gif or .jpg. Everything I have read says this option can not be done by design (security reasons). I find that irronic...
6
by: Kay | last post by:
Hi all, In vb6, I can define a custom type like this: Private Type uClient sName As String sMonday As Double sMondayHeadCnt As Integer End Type
3
by: Wayne | last post by:
I downloaded the Office 2007 Beta today and have been clicking around Access and the other Office Apps. They all have clear type switched on despite the fact that I have it turned off in Windows. ...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
3
by: Lord0 | last post by:
I am trying to implement variable content containers using an abstract type and type substitution. My schema is as follows: <?xml version="1.0" encoding="UTF-8"?> <schema...
1
by: mato81 | last post by:
Hi all! I am a newbie to WSDL. I have a questions which has been driving me crazy... If I would have a WSDL with a types element somewhat like below, what is the point of the third last row...
7
by: Robert | last post by:
Thanks George, I really am grateful for attempts to be helpful, but this really doesn't answer the question in my OP. What I am looking for is an explanation of WHY things are this way (I was...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.