关于匿名类型的问题 [英] question on anonymous type

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

问题描述

大家好,

我对匿名类型有疑问


我可以写:

使用(StreamWriter writer =新的StreamWriter(...))


我也可以写这个:

使用(var writer = new StreamWriter(...))


什么是最好的?我应该选择哪个选项?

第一个似乎更具可读性...


提前感谢您的回答

Hi all,
I have a question on anonymous type

I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...

Thanks in advance for your answer

推荐答案

6月3日上午11:03,timor.su ... @ gmail.com写道:
On Jun 3, 11:03 am, timor.su...@gmail.com wrote:

大家好,


我对匿名类型有疑问


我可以写:

使用(StreamWriter writer) = new StreamWriter(...))


我也可以写这个:

using(var writer = new StreamWriter(...))


什么是最好的?

我应该选择哪个选项?
Hi all,

I have a question on anonymous type

I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best?
Which option should I choose ?



这通常是品味问题。

It''s a matter of taste usually.


第一个似乎更具可读性...... 。
The first seems to be more readable ...



然后使用它。就我个人而言,我不喜欢在写作StreamWriter的过程中涉及的复制。两次(当我想说

Dictionary< string,List< Entry>或者其他一些同样复杂的

泛型时,它会变得更糟)......但它是'由你决定。


注意当你开始使用LINQ

表达式时,var变得更加重要(其中实际类型可能很难编写

down)。

Then use that. Personally, I don''t like the duplication involved in
writing "StreamWriter" twice (and it gets worse when I want to say
Dictionary<string,List<Entry>or some other equally complicated
generic) ... but it''s up to you.

Note that var becomes more important when you start using LINQ
expressions (where the actual types can be rather difficult to write
down).


我可以写:
I can write :

using(StreamWriter writer = new StreamWriter (...))


我也可以写这个:

使用(var writer = new StreamWriter(...))


什么是最好的?我应该选择哪个选项?
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?



在这种情况下,除了var之外,我认为两者都没有任何好处。打字速度更快。

In this case I see no benefits in either, except "var" is quicker to type.


6月3日上午11:03,timor.su ... @ gmail.com写道:
On Jun 3, 11:03 am, timor.su...@gmail.com wrote:

我有一个关于匿名类型的问题
I have a question on anonymous type



实际上,你对隐式输入的局部变量有疑问。

匿名类型是您编写如下代码时创建的类型:


new {Name =" Jon"这两个功能经常一起使用,但不一定要用。

Actually, you have a question on implicitly typed local variables.
Anonymous types are the types created when you write code like this:

new { Name="Jon" }

The two features are often used together, but don''t have to be.


我可以写:

使用(StreamWriter writer = new StreamWriter(...))


我也可以写这个:

使用( var writer = new StreamWriter(...))


什么是最好的?我应该选择哪个选项?

第一个似乎更具可读性...
I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...



嗯,第一个包含冗余信息 - 但它'更多

expicit。这主要是品味问题。我发现自己

使用隐式类型的局部变量而且没有损失

可读性。


Jon

Well, the first contains redundant information - but it''s more
expicit. It''s largely a matter of taste though. I''ve found myself
using implicitly typed local variables quite a bit with no loss of
readability.

Jon


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

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