C#初始化问题 [英] C# initializer questions

查看:84
本文介绍了C#初始化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的特色是什么?我意识到它正在初始化对象

C#规范中称为什么?



员工JohnDoe =新员工()

{

firstname =john;

lastname =doe;

age = 200;

h1visa = false;

};



但这与初始化默认构造函数的C ++方式的原因是什么?br />


员工JohnDoe =新员工(john,doe,200,false)





提前感谢

解决方案

MSDN非常清楚: https://msdn.microsoft.com/en-us/library/bb384062.aspx?f=255&MSPPError=-2147217396 [ ^ ]



对象初始化器让你屁股在创建时将值写入对象的任何可访问字段或属性,而不必调用构造函数后跟赋值语句行。使用对象初始值设定语法可以指定构造函数的参数或省略参数(和括号语法)。

...

虽然对象初始值设定项可以在任何上下文中使用,但它们在LINQ查询表达式中特别有用。查询表达式经常使用匿名类型,只能通过使用对象初始化程序初始化


请参阅我对该问题的评论。我真的不明白什么很难找出术语。它只是被称为初始化器,没有什么特别的;这是你自己已经使用过的术语(归功于你):):https://msdn.microsoft.com/en-us/library/bb384062.aspx [ ^ ]。



不是这种语法的好处是显而易见的?首先,它可以被认为是一些等价的传递参数按名称,但没有任何参数。坦率地说,快速将调用写入一个多于一个的构造函数一些参数有点麻烦,特别是如果有许多不同的构造函数具有不同的数量参数,即使你使用Intellisense;您需要按特定顺序编写所有参数,并且必须比较不同的构造函数以确定您真正需要的那个,通常是在填充这些参数的过程中。



使用更新的语法,您可以按任意顺序编写某些属性的值集。当您查看自己或他人的代码时,您可以清楚地看到哪个属性属于哪个属性。在设计构造函数时,可以避免构造函数包含太多参数,只留下最重要的情况。即使您出于某种原因,在构造函数中编译了针对早期C#版本的代码,您仍然可以使用赋值语句,但是使用这种初始化形式的语言的用户可以真正享受它的好处。



真的,我觉得很难解释这个功能的基本原理。它类似于向那些无法得到它的人解释笑话的尴尬感 - 而不是最好的活动。正如我所说,好的功能不需要借口。



-SA


谢谢



任意订单是我不知道的关键功能



谢谢

What feature of C# is this? I realize it is initializing objects
What is this called in the C# specification?

Employee JohnDoe = new Employee()
{
firstname = "john";
lastname = "doe";
age = 200;
h1visa = false;
};

but what is the reason for this vs the C++ way of initializing default constructors

Employee JohnDoe = new Employee(john, doe, 200, false)


thanks in advance

解决方案

MSDN covers this pretty well: https://msdn.microsoft.com/en-us/library/bb384062.aspx?f=255&MSPPError=-2147217396[^]

"Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).
...
Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of anonymous types, which can only be initialized by using an object initializer"


Please see my comment to the question. I really don't understand what would be so hard to find out the terminology. It's just called "initializer", nothing special; this is the term you already used yourself (credit to you :)): https://msdn.microsoft.com/en-us/library/bb384062.aspx[^].

Aren't the benefits of this syntax obvious? First of all, it can be considered as some equivalent of passing parameters by name, without having any parameters, though. Frankly, quickly writing the call to a constructor with more than a couple of parameters is somewhat bothering, especially if there are many different constructors with different number of parameters, even if you use Intellisense; you need to write all parameters in certain order, and have to compare different constructors to make a decision which one you really need, often in the middle of the process of filling in those parameters.

With newer syntax, you can write the set of values of some properties in arbitrary order. When you look at your own or someone else's code, you can clearly see which value goes to what property. When you design the constructors, you can avoid constructors with too many parameters, leaving only the most important cases. Even when you, by some reason, compile the code targeting the earlier C# versions when this constructor, you can always leave the user with the opportunity to assign the property values, not passed as constructor parameters, using assignment statements, but the user of the language with this newer form of the initializer can really enjoy it's benefits.

Really, I feel uncomfortable explaining the rationale of the feature. It resembles the awkward feeling of explaining the joke to those who cannot get it — not the best activity. As I said, "good feature does not need excuses".

—SA


thanks

"arbitrary order" is the key functionality I didn't know about

thanks


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

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