c#v3.0:隐式变量声明,为什么? [英] c# v3.0: implicit variable declaration, why?

查看:52
本文介绍了c#v3.0:隐式变量声明,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我和我合作的其他开发人员正在研究v3.0

规格。扩展看起来很棒,购买带有'var'类型的隐含

变量的点是什么?


然后再打击我,保持记住,我们是德尔福商店,搬到c#

所以我们还没有在那里太多,但这似乎有点像

无意义。如果你必须在申报时进行初始化,为什么不直接输入

以便于阅读?


我们在msdn上观看了视频,但仍然没有相当明白......


有人可以给我一个真实世界的例子,说明这有用吗?

so, myself and the other developers i work with were looking at the v3.0
specifications. extensions look great, buy what is the point of implicit
variables with the ''var'' type?

before you bash me too much, keep in mind, we''re a delphi shop, moving to c#
so we''ve not been in there too much yet, but this seems to be kind of
pointless. if you have to initialize when you declare, why not just type it
for readability sake?

we watched the video on msdn, but still don''t quite get it...

can someone give me a real world example of where this is useful?

推荐答案

Nathan,


这是一个合理的问题。简短的回答是,它只是C#3.0提供的更大功能集中的一部分。现在

到了很长的答案。


所以,使用隐式变量你当然可以这样做:


var i = 10; // int

var s =" hello" ;; //字符串


如果我看到有人为我工作那么,那么我会解雇他们

(也许不是在真正长时间参数化的情况下/通用类型,但

使用指令也可以正常工作。


但是,在C#3.0中,您可以创建匿名类型,如这个:


var x = new {Prop1 =" Hello",Prop2 = 10};


语法可能在这里,但你应该得到一般的想法。现在,

当你这样做时,编译器正在创建一个具有Prop1属性的类型和

一个Prop2属性,具有非常特定的类型签名。但是,在
代码时间,您无法访问该类型,这就是var的用途。


最棒的是一旦你这样做,你就不能改变

var的类型,它肯定是打字的。所以这是类型安全的。你只能分配另一种类型的

实例。


现在,这个功能本身很棒,IMO,但这不是全部。

这实际上是用于在LINQ中实现预测的内容。既然

你可以做这样的匿名类型,你可以选择你想从你的查询中返回什么

属性。


这当然会导致您的查询返回值

IEnumerable< anonymous typewhereanonymous type包含您在查询中选择返回的属性




希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse。 com

" Nathan Laff" < nl *** @ comcast.netwrote in message

news:c5 *************************** ***@comcast.com。 ..
Nathan,

It''s a reasonable question. The short answer is that it is just one
part in a much larger set of features that are offered with C# 3.0. Now
onto the long answer.

So, with implicit variables you can certainly do this:

var i = 10; // int
var s = "hello"; // string

And if I ever saw someone working for me do that, then I would fire them
(maybe not in the case of really long parameterized/Generic types, but a
"using" directive works just as well).

However, in C# 3.0, you can create anonymous types, like this:

var x = new {Prop1 = "Hello", Prop2 = 10};

The syntax might be off here, but you should get the general idea. Now,
when you do this, the compiler is creating a type with a Prop1 property and
a Prop2 property, with a very specific type signature. However, at
code-time, you don''t have access to that type, which is what the var is for.

The great thing is that once you do that, you can''t change the type of
var, it is definitely typed. So it is type safe. You can only assign an
instance of another type.

Now, this feature in itself is great, IMO, but that''s not all of it.
This is actually what is used to implement projections in LINQ. Now that
you can do anonymous types like this, you can pick and choose what
properties you want to return from your queries.

This of course results in a return value from your queries of
IEnumerable<anonymous typewhere "anonymous type" contains the properties
you selected in your query to return.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Nathan Laff" <nl***@comcast.netwrote in message
news:c5******************************@comcast.com. ..

所以,我和我合作的其他开发人员正在查看v3.0

规格。扩展看起来很棒,购买带有'var'类型的隐含

变量的点是什么?


然后再打击我,保持记住,我们是德尔福的商店,转移到了
c#所以我们还没有去过那么多,但这似乎有点像

无意义。如果你必须在声明时进行初始化,为什么不只是为了可读性而键入




我们在msdn上观看视频,但仍然没有''相当明白......


有人可以给我一个真实世界的例子说明这有用吗?
so, myself and the other developers i work with were looking at the v3.0
specifications. extensions look great, buy what is the point of implicit
variables with the ''var'' type?

before you bash me too much, keep in mind, we''re a delphi shop, moving to
c# so we''ve not been in there too much yet, but this seems to be kind of
pointless. if you have to initialize when you declare, why not just type
it for readability sake?

we watched the video on msdn, but still don''t quite get it...

can someone give me a real world example of where this is useful?


< br>

" Nathan Laff" < nl *** @comcast.netaécritdansle message de news:
C5 ************ @ comcast.com ...


|所以,我和我合作的其他开发人员都在关注v3.0

|规格。扩展看起来很棒,买什么是隐含的点

| ''var''类型的变量?

|

|在你抨击我之前,请记住,我们是德尔福商店,转移到

c#

|所以我们还没有在那里太多,但这似乎是一种

|无意义。如果你必须在申报时进行初始化,为什么不输入



|为了便于阅读?


隐式变量有点像类帮助程序,它们主要是为了提供框架功能的设施而支持



隐式变量的主要原因是允许像Linq

这样的代码查询。对象列表或数据库可以返回

强类型结果到查询中声明的变量。


{

var结果=来自客户的c

其中c.ContactTitle.Length == 5

选择c.ContactName;

}


此示例将从Customer

对象列表中选择单个字符串属性ContactName,其中ContactTitle属性的

长度为5个字符。编译器知道该属性是一个字符串,因此

使var类型为字符串。


注意这与变体无关; var声明是强烈的

在第一次分配时输入并从那时起保持该类型。他们

不能改变类型。


{

var result =来自客户的c

其中c.ContactTitle.Length == 5

选择c.ContactName,c.Age,c.CurrentBalance;

}


这种查询将返回一个包含三个成员的类,一个用于查询的select语句中的每个

属性。类似于:


class结果

{

string ContactName;

int Age;

十进制CurrentBalance;

}


乔安娜


-

Joanna Carter [TeamB]

顾问软件工程师
"Nathan Laff" <nl***@comcast.neta écrit dans le message de news:
c5******************************@comcast.com...

| so, myself and the other developers i work with were looking at the v3.0
| specifications. extensions look great, buy what is the point of implicit
| variables with the ''var'' type?
|
| before you bash me too much, keep in mind, we''re a delphi shop, moving to
c#
| so we''ve not been in there too much yet, but this seems to be kind of
| pointless. if you have to initialize when you declare, why not just type
it
| for readability sake?

Implicit variables are a bit like class helpers, they are there, primarily,
to provide facility for framework functionality.

The main reason for implicit variables is to allow for things like Linq
where a code "query" against a list of objects or a database can return
strongly typed results into variables declared in the query.

{
var result = from c in Customers
where c.ContactTitle.Length == 5
select c.ContactName;
}

This example will select the single string property ContactName where the
length of the ContactTitle property is 5 characters, from a list of Customer
objects. The compiler knows that the property is a string and therefore
makes the var type to be string.

Note that this is nothing to do with variants; var declarations are strongly
typed on their first assignment and remain that type from then on. They
cannot change type.

{
var result = from c in Customers
where c.ContactTitle.Length == 5
select c.ContactName, c.Age, c.CurrentBalance;
}

This kind of query will return a class with three members, one for each
property in the select statement of the query. Something like :

class Result
{
string ContactName;
int Age;
decimal CurrentBalance;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


在我看来,我们正在倒退这个....朝向

JavaScript / VBScript == Bad?

" Nicholas Paldino [。NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:uQ ************* @ TK2MSFTNGP05.phx.gbl ...
Seems to me that we''re going backwards on this.... towards
JavaScript/VBScript == Bad?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uQ*************@TK2MSFTNGP05.phx.gbl...

Nathan,


这是一个合理的问题。简短的回答是,它只是C#3.0提供的更大功能集中的一部分。现在

到了很长的答案。


所以,使用隐式变量你当然可以这样做:


var i = 10; // int

var s =" hello" ;; //字符串


如果我看到有人为我工作那么,那么我会解雇

他们(也许不是在很长时间参数化的情况下/泛型类型,

但是using指令也可以正常工作。


但是,在C#3.0中,你可以创建匿名类型,比如这个:


var x = new {Prop1 =" Hello",Prop2 = 10};


语法可能在这里,但是你应该得到一般的想法。

现在,当你这样做时,编译器正在创建一个具有Prop1

属性和Prop2属性的类型,具有非常具体的类型签名。

但是,在代码时,您无法访问该类型,这就是var的用途。




最棒的是,一旦你这样做,就不能改变

var的类型,它肯定是打字的。所以这是类型安全的。你只能分配另一种类型的

实例。


现在,这个功能本身很棒,IMO,但这不是全部。

这实际上是用于在LINQ中实现预测的内容。既然

你可以做这样的匿名类型,你可以选择你想从你的查询中返回什么

属性。


这当然会导致您的查询返回值

IEnumerable< anonymous typewhereanonymous type包含您在查询中选择返回的属性




希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam。 guard.caspershouse.com

" Nathan Laff" < nl *** @ comcast.netwrote in message

news:c5 *************************** ***@comcast.com。 ..
Nathan,

It''s a reasonable question. The short answer is that it is just one
part in a much larger set of features that are offered with C# 3.0. Now
onto the long answer.

So, with implicit variables you can certainly do this:

var i = 10; // int
var s = "hello"; // string

And if I ever saw someone working for me do that, then I would fire
them (maybe not in the case of really long parameterized/Generic types,
but a "using" directive works just as well).

However, in C# 3.0, you can create anonymous types, like this:

var x = new {Prop1 = "Hello", Prop2 = 10};

The syntax might be off here, but you should get the general idea.
Now, when you do this, the compiler is creating a type with a Prop1
property and a Prop2 property, with a very specific type signature.
However, at code-time, you don''t have access to that type, which is what
the var is for.

The great thing is that once you do that, you can''t change the type of
var, it is definitely typed. So it is type safe. You can only assign an
instance of another type.

Now, this feature in itself is great, IMO, but that''s not all of it.
This is actually what is used to implement projections in LINQ. Now that
you can do anonymous types like this, you can pick and choose what
properties you want to return from your queries.

This of course results in a return value from your queries of
IEnumerable<anonymous typewhere "anonymous type" contains the properties
you selected in your query to return.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Nathan Laff" <nl***@comcast.netwrote in message
news:c5******************************@comcast.com. ..

>所以,我和我合作的其他开发人员正在研究v3.0
规范。扩展看起来很棒,购买带有'var'类型的隐含变量的点是什么?

在你打击我之前,请记住,我们是一个delphi购物,转移到
c#所以我们还没有在那里太多,但这似乎有点无意义。如果你必须在声明时进行初始化,为什么不只是为了可读性而键入它?

我们在msdn上观看视频,但仍然不太明白...

有人能给我一个真实世界的例子说明这有用吗?
>so, myself and the other developers i work with were looking at the v3.0
specifications. extensions look great, buy what is the point of implicit
variables with the ''var'' type?

before you bash me too much, keep in mind, we''re a delphi shop, moving to
c# so we''ve not been in there too much yet, but this seems to be kind of
pointless. if you have to initialize when you declare, why not just type
it for readability sake?

we watched the video on msdn, but still don''t quite get it...

can someone give me a real world example of where this is useful?




这篇关于c#v3.0:隐式变量声明,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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