推断一种类型 [英] inferring a type

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

问题描述

这是我不久前发生的事情,我昨晚再一次想到这个问题。我注意到它似乎是多余的类似

这样的表达式:

SomeType x = new SomeType();


我问自己,你为什么要两次输入SomeType?我意识到

首先是一个类名,第二个是构造函数,但它仍然是
有点多余。


无论如何,巧合的是,我在MSDN上发现了一段视频,其中Anders说了很多关于他们的事情,他碰巧提到了这一点。他说b $ b他们考虑用C#做的一件事是这种可能的语法:


var x = new SomeType();


和x的类型将从''new''表达式中推断出来。我注意到这个视频是在2004年发布的,因此我很想知道,从那时起,这种语言是否已经悄悄进入该语言?如果不是,为什么不呢?


谢谢。

This is something that occurred to me a while ago, and again I was
thinking about it last night. I noticed how redundant it seems to type
an expression like this:

SomeType x = new SomeType();

I asked myself, why do you have to type SomeType twice? I realize the
first is a class name and the second is a constructor, but still it''s a
little redundant.

Anyway, coincidentally, I found a video on MSDN with Anders speaking
about a bunch of things, and he happened to bring up this very point. He
said one thing they were considering for C# was this possible syntax:

var x = new SomeType();

and the type of x would be inferred from the ''new'' expression. I noticed
that the video was posted in 2004, so I''m wondering, is this some
feature that has snuck into the language since then? If not, why not?

Thanks.

推荐答案

John,
John,
我注意到这个视频是在2004年发布的,所以我很想知道,从那时起,这种语言是否已经悄悄进入该语言?如果没有,为什么不呢?
I noticed
that the video was posted in 2004, so I''m wondering, is this some
feature that has snuck into the language since then? If not, why not?




还没有,但这是他们计划在C#中添加的功能之一

v3 。

Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



Not yet, but it''s one of the features they are planning to add in C#
v3.
Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


你不能这样做吗?

对象x =新的SomeType();

也许它与在脚本语言中使用var x并不完全相同,

但它非常接近。


但是它会让你很难弄清楚你在哪个界面实施。 SomeType可以转换为它的继承类型,或其他实现的接口,你必须在那时键入对象

无论如何,我我宁愿让intellisense帮助我。我很好奇

这个功能是如何工作的,如果实际上他们正在考虑它的第3版

C#。


我在讨论你为什么要讨论为什么你不能在开关语句中使用某种类型的某种类型的时候从某个地方读过一个精彩的引语,有点像有时候

通过移除或不设计特征来实现最佳设计。 (我

我认为我引用了这句话...... :)


" John Salerno" <乔****** @ NOSPAMgmail.com>在消息中写道

news:34 ******************** @ rcn.net ...
can''t you already do that?
object x = new SomeType();
Perhaps it''s not quite the same as using var x as in scripting languages,
but it''s pretty close.

But it would make it difficult to figure out which interface you are
implementing. SomeType can be cast to it''s inheriting types, or other
implemented interfaces, and you''ll have to type the object at that point
anyways, and I''d rather have intellisense help me out. I would be curious
how this feature would work, if in fact they are thinking about it for v3 of
C#.

I read a brilliant quote once from somewhere during a discussion of why you
can''t use a type in a switch statement that was something like "Sometimes
the best design is achieved by removing or not designing a feature." (I
think I butured the quote... :)

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:34********************@rcn.net...
这个是我刚才发生的事情,昨晚我再一次想到它。我注意到输入这样的
表达似乎多余:

SomeType x = new SomeType();

我问自己,你为什么要这样做输入SomeType两次?我意识到
首先是一个类名,第二个是构造函数,但它仍然没什么多余的。

无论如何,巧合的是,我在MSDN上发现了一个视频安德斯谈到了很多事情,他碰巧提到了这一点。他说他们考虑C#的一件事是这种可能的语法:

var x = new SomeType();

和x的类型将被推断来自''新''的表达。我注意到这个视频是在2004年发布的,所以我很想知道,从那时起,这个功能是否已经悄悄进入语言?如果没有,为什么不呢?

谢谢。
This is something that occurred to me a while ago, and again I was
thinking about it last night. I noticed how redundant it seems to type an
expression like this:

SomeType x = new SomeType();

I asked myself, why do you have to type SomeType twice? I realize the
first is a class name and the second is a constructor, but still it''s a
little redundant.

Anyway, coincidentally, I found a video on MSDN with Anders speaking about
a bunch of things, and he happened to bring up this very point. He said
one thing they were considering for C# was this possible syntax:

var x = new SomeType();

and the type of x would be inferred from the ''new'' expression. I noticed
that the video was posted in 2004, so I''m wondering, is this some feature
that has snuck into the language since then? If not, why not?

Thanks.



John,


这个与提议的东西没什么关系。有你所拥有的,我

可以做到这一点:


对象x =新的SomeType();

x = 1;

x =" hello";


使用var,我不能这样做。另外,对于var,我将支持

intellisense,因为变量是强类型的。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" John Richardson" < J3 ********** @ hotmail.com>在消息中写道

新闻:eR ************** @ TK2MSFTNGP09.phx.gbl ...
John,

This is nothing close to what var is proposing. With what you have, I
can do this:

object x = new SomeType();
x = 1;
x = "hello";

With var, I can not do that. Additionally, with var, I will have
intellisense support, since the variable is strongly typed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Richardson" <j3**********@hotmail.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
你不能吗?已经这样做了吗?
对象x =新的SomeType();
也许它与使用脚本语言中的var x并不完全相同,
但它非常接近。

但这会让你很难弄清楚你正在实施哪个界面。 SomeType可以转换为它的继承类型,或其他实现的接口,并且你必须在那时键入对象
,我宁愿有intellisense帮助我出。我很好奇
这个功能是如何工作的,如果实际上他们正在为C#的v3考虑它。

我在讨论时从某个地方读过一个精彩的引用为什么
你不能在switch语句中使用类似
的类型?有时候最好的设计是通过删除或不设计
功能来实现的。 (我想我已经引用了这句话......)

John Salerno <乔****** @ NOSPAMgmail.com>在消息中写道
新闻:34 ******************** @ rcn.net ...
can''t you already do that?
object x = new SomeType();
Perhaps it''s not quite the same as using var x as in scripting languages,
but it''s pretty close.

But it would make it difficult to figure out which interface you are
implementing. SomeType can be cast to it''s inheriting types, or other
implemented interfaces, and you''ll have to type the object at that point
anyways, and I''d rather have intellisense help me out. I would be curious
how this feature would work, if in fact they are thinking about it for v3
of C#.

I read a brilliant quote once from somewhere during a discussion of why
you can''t use a type in a switch statement that was something like
"Sometimes the best design is achieved by removing or not designing a
feature." (I think I butured the quote... :)

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:34********************@rcn.net...
这是发生的事情我前一阵子,昨晚我又在想着它。我注意到输入这样的
表达似乎多余:

SomeType x = new SomeType();

我问自己,你为什么要这样做输入SomeType两次?我意识到
首先是一个类名,第二个是构造函数,但它仍然没什么多余的。

无论如何,巧合的是,我在MSDN上发现了一个视频安德斯谈到了很多事情,他碰巧提到了这一点。他说他们为C#考虑的一件事是这种可能的语法:

var x = new SomeType();

和x的类型将被推断来自''新''的表达。我注意到这个视频是在2004年发布的,所以我很想知道,从那时起,这个功能是否已经悄悄进入语言?如果没有,为什么不呢?

谢谢。
This is something that occurred to me a while ago, and again I was
thinking about it last night. I noticed how redundant it seems to type an
expression like this:

SomeType x = new SomeType();

I asked myself, why do you have to type SomeType twice? I realize the
first is a class name and the second is a constructor, but still it''s a
little redundant.

Anyway, coincidentally, I found a video on MSDN with Anders speaking
about a bunch of things, and he happened to bring up this very point. He
said one thing they were considering for C# was this possible syntax:

var x = new SomeType();

and the type of x would be inferred from the ''new'' expression. I noticed
that the video was posted in 2004, so I''m wondering, is this some feature
that has snuck into the language since then? If not, why not?

Thanks.




这篇关于推断一种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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