简体   繁体   English

匿名类型VS局部变量,何时应该使用?

[英]Anonymous types VS Local variables, When should one be used?

I am not sure when I should use anonymous types instead of local variables in C#. 我不确定何时应该在C#中使用匿名类型而不是局部变量

I have: 我有:

string  fullMessage // This is the full message including sender and recipient names

string sender = GetMessagePart(fullMessage, "from");
string recipient = GetMessagePart(fullMessage, "to");

//do some stuff and deliver the message

Should I use: 我应该使用:

var msg = new { 
sender = GetMessagePart(fullMessage, "from")
recipient = GetMessagePart(fullMessage, "to")
};

Instead? 代替?

Do you mean statically typed variables? 你的意思是静态类型的变量? Note that anonymous types are statically typed... (removed due to question edit) 请注意,匿名类型静态类型的... (由于问题编辑而被删除)

There are 2 problems with C# anonymous types: C#匿名类型有2个问题:

  • you can't expose them through a method API 你不能通过方法API公开它们
  • you can't mutate them (the members are read-only) 你不能改变它们(成员是只读的)

If you only need to know about the data within a single method, and it is read-only, then an anonymous type is handy (and this covers a lot of cases, in reality). 如果您只需要了解单个方法中的数据,并且它是只读的,那么匿名类型就很方便(实际上这涵盖了很多情况)。

If you need to mutate the data or pass it out to a caller, then use either a bespoke class, or simple variables (etc). 如果您需要改变数据或将其传递给调用者,则使用定制类或简单变量(等)。

In the case given, I can't see a reason to use an anonymous type; 在给出的情况下,我看不出使用匿名类型的理由; if you just want the values, use the separate variable approach. 如果您只想要这些值,请使用单独的变量方法。 If a "message" has a defined meaning, declare a Message class and populate that. 如果“消息”具有已定义的含义,则声明Message类并填充该类。

Does grouping a sender and recipient together make sense outside this method? 在此方法之外将发件人和收件人组合在一起是否有意义? If so, consider creating a class for them. 如果是这样,请考虑为他们创建一个类。 If not, I would usually use separate local variables, but I suspect that's mostly through habit. 如果没有,我通常会使用单独的局部变量,但我怀疑这主要是通过习惯。

I suspect what we've here got is a pair of local variables which are conceptually related. 我怀疑我们在这里得到的是一对概念上相关的局部变量。 The relationship may not be strong enough to deserve a full type, but it's meaningful within the method. 这种关系可能不够强大,不足以得到一个完整的类型,但它在方法中是有意义的。 In some ways, using an anonymous type is a very neat way of making that pairing obvious. 在某些方面,使用匿名类型是一种非常巧妙的方式,使配对显而易见。 On the other hand, if your method is long enough that it really needs that extra level of clarity, perhaps you should break it up anyway. 另一方面,如果你的方法足够长,确实需要额外的清晰度,也许你应该打破它。

Note that using an anonymous type makes some refactoring techniques harder because the type is only available with the method (without a bit of hackery). 请注意,使用匿名类型会使一些重构技术更难,因为该类型仅适用于该方法(没有一点hackery)。

I realise that this is a wishy-washy answer, but it does strike me that there's some merit in the overall idea - it's a bit like using a tuple in a functional language. 我意识到这是一个多余的回答,但它确实让我觉得整体想法有一些优点 - 这有点像在函数式语言中使用元组。

Use local variables (i think this is what you meant) in this case. 在这种情况下,使用局部变量(我认为这就是你的意思)。

Anonymous type should be use where you would need a standard name type but use it only for implementation purpose inside of a method. 匿名类型应该在需要标准名称类型的地方使用,但仅用于方法内部的实现目的。 It removes the tedious work of creating a new type definition. 它消除了创建新类型定义的繁琐工作。

You would not need a type here, so don't use an anonymous type. 你不需要这里的类型,所以不要使用匿名类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM