C#3.0中的隐式类型,它们的用处是什么? [英] implicit types in C# 3.0, what is the usefulness of them?

查看:65
本文介绍了C#3.0中的隐式类型,它们的用处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#3.0规范( http://msdn.microsoft.com/vcsharp / future / )包含名为隐式类型的局部变量的

功能。


变量的类型在编译时确定基于=符号右边的

表达式:


var s =" Hello" ;; // s被强类型为字符串

var i = 25; //我被强类型为整数


但是我不明白它有什么优势?


string s = "你好英寸; //这很容易打字,更容易

阅读


这是什么故事?

解决方案

我想这将是为了更多的多态代码...?那是我的最好的猜测,好像我曾经看过有人在任何正常的代码中使用它我会b / b
打它们......这当然是假设我做的没有听到任何

其他有效理由。


Chris,


引入它的原因是支持LINQ,用于对
查询进行排列。基本上,当对可枚举类型执行查询时,您可以选择要返回的内容。例如,假设您有一个具有三个

属性的类,FirstName,LastName和Id。当您执行查询时,您可以选择返回仅包含Id和LastName的类型。
。事情是,

你不知道那种类型是什么(好吧,你这样做,但创建一个

兼容类型是一个痛苦,如果你不得不做它一直都是。


这就是var的用武之地。它与这些匿名类型一起使用

允许隐式输入(毕竟,是什么你会用什么类型的

匿名类型。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard。 caspershouse.com


" Chris Dunaway" <杜****** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

C#3.0规范( http://msdn.microsoft.com/ vcsharp / future / )包含一个名为隐式类型的局部变量的
特性。

变量的类型是在编译时基于
确定的。 =符号右侧的表达式:

var s =" Hello" ;; // s强类型为字符串
var i = 25; //我被强类型化为一个整数

但是我不明白它有什么优势呢?

string s =" Hello" ;; //这很容易打字,更容易阅读

故事是什么?



< blockquote> gmiley,


查看我的回复,告诉我你认为它是否有效;)


我同意,如果你知道类型事先,然后是的,你不应该使用

它,但如果你不知道这种类型(请参阅我的回答为什么你不会知道b
) ,你需要这个。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" gmiley" < GM **** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

我想这将是为了更多的多态代码...?这是我最好的猜测,好像我曾经看到有人在任何正常的代码中使用它我会砸他们......这当然是假设我没有听到任何
其他有效理由。



The C# 3.0 spec (http://msdn.microsoft.com/vcsharp/future/) contains a
feature called "Implicitly typed local variables".

The type of the variable is determined at compile time based on the
expression to the right of the = sign:

var s = "Hello"; //s is strongly typed to be a string
var i = 25; //i is strongly typed to be an integer

But what I don''t understand what advantage it is?

string s = "Hello"; //This is just as easy to type and is easier to
read

What''s the story?

解决方案

I guess it would be for the sake of more polymorphic code...? That''s my
best guess as if I ever saw someone use that in any normal code I would
smack them... this is of course assuming that I do not hear of any
other valid reason for it.


Chris,

The reason this was introduced was to support LINQ, for permutations on
queries. Basically, when performing a query on an enumerable type, you can
choose what to return. For example, say you have a class with three
properties, FirstName, LastName, and Id. When you perform a query, you can
choose to return a type with just the Id and the LastName. The thing is,
you don''t know what that type will be (well, you do, but creating a
compatable type is a pain if you have to do it all the time).

This is where var comes in. It is used along with these anonymous types
to allow implicit typing (after all, what type would you use for the
anonymous type you just created).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

The C# 3.0 spec (http://msdn.microsoft.com/vcsharp/future/) contains a
feature called "Implicitly typed local variables".

The type of the variable is determined at compile time based on the
expression to the right of the = sign:

var s = "Hello"; //s is strongly typed to be a string
var i = 25; //i is strongly typed to be an integer

But what I don''t understand what advantage it is?

string s = "Hello"; //This is just as easy to type and is easier to
read

What''s the story?



gmiley,

See my response, tell me if you think it is valid ;)

I agree, if you know the type beforehand, then yes, you should not use
it, but if you don''t know the type (see my response for why you wouldn''t
know), you will need this.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gmiley" <gm****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

I guess it would be for the sake of more polymorphic code...? That''s my
best guess as if I ever saw someone use that in any normal code I would
smack them... this is of course assuming that I do not hear of any
other valid reason for it.



这篇关于C#3.0中的隐式类型,它们的用处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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