[英]Constructor based object initialization
Are there significant performance differences (at runtime and/or compile time) between constructing and assigning the properties of a class like this: 在构造和分配这样的类的属性之间(在运行时和/或编译时)是否存在明显的性能差异:
Employee currentUser = new Employee();
currentUser.Name = "Bob";
or like this: 或像这样:
Employee currentUser = new Employee() { Name = "Bob" };
My actual example is not that simple, the properties of the class are actually assigned to some long linq expressions. 我的实际例子不是那么简单,实际上是将类的属性分配给一些长的linq表达式。
I searched Google and Stack Overflow for answers but i only found questions regarding best practices, when to use either method, and not anything performance related. 我在Google和Stack Overflow上搜索了答案,但我仅发现有关最佳做法,何时使用这两种方法的问题,而与性能无关。
Apologies in advance if i'm asking a silly question. 如果我问一个愚蠢的问题,提前致歉。
No. This is only a syntactic sugar. 不,这只是一种语法糖。 The IL generated will be the same (Will be updating with IL)
生成的IL将相同(将使用IL更新)
internal class Foo
{
public string Bar { get; set; }
}
class Program
{
static void Main(string[] args)
{
Foo f = new Foo()
{
Bar = "Baaz"
};
Console.WriteLine("Now the old way sugar");
Foo f2 = new Foo();
f2.Bar = "Baaz";
}
}
Now IL: 现在IL:
IL_0000: nop
IL_0001: newobj instance void SimpleDataPlayGround.Foo::.ctor()
IL_0006: stloc.2
IL_0007: ldloc.2
IL_0008: ldstr "Baaz"
IL_000d: callvirt instance void SimpleDataPlayGround.Foo::set_Bar(string)
IL_0012: nop
IL_0013: ldloc.2
IL_0014: stloc.0
IL_0015: ldstr "Now the old way sugar"
IL_001a: call void [mscorlib]System.Console::WriteLine(string)
IL_001f: nop
IL_0020: newobj instance void SimpleDataPlayGround.Foo::.ctor()
IL_0025: stloc.1
IL_0026: ldloc.1
IL_0027: ldstr "Baaz"
IL_002c: callvirt instance void SimpleDataPlayGround.Foo::set_Bar(string)
IL_0031: nop
IL_0032: ret
OK, after comments from Jon, it is obvious that there is a slight difference . 好的,在乔恩发表评论之后,很明显两者之间存在细微的差别 。 But as for the question - mainly performance implications - there is none but as Jon points out, this can be important if an object is used for its own re-creation.
但是对于这个问题-主要是性能方面的问题-正如乔恩(Jon)指出的那样,如果将对象用于自己的重新创建,这可能很重要。
The two aren't quite the same. 这两个是不太一样的。 The object initializer form translates into something more like this (at least logically; I believe there are some cases where the C# compiler will just use a single variable):
对象初始化程序的形式转换为类似这样的内容(至少在逻辑上;我相信在某些情况下C#编译器将只使用一个变量):
Employee tmp = new Employee();
tmp.Name = "Bob";
Employee currentUser = tmp;
This is particularly important if you're ever changing the value of the variable. 如果您要更改变量的值,这尤其重要。 For example:
例如:
Employee currentUser = new Employee { Name = "Bob" };
currentUser = new Employee { Name = currentUser.Name + "Foo" };
Here the result would be an employee with a name of "BobFoo", not just "Foo" which is what you'd get if the assignment to the variable occurred before the property setter. 在这里,结果将是一个名为“ BobFoo”的员工, 而不仅仅是“ Foo”,如果对变量的赋值发生在属性设置器之前 ,则您将获得该名称。
This could be important in other situations where the current value of the variable is read, eg in the property setter. 这在读取变量当前值的其他情况下(例如在属性设置器中)可能很重要。 Unlikely, but possible.
不太可能,但可能。
See section 7.6.10.2 for the details of this, where it's explicitly specified in terms of an "invisible and inaccessible temporary variable". 有关详细信息,请参见7.6.10.2节,其中根据“不可见和不可访问的临时变量”对其进行了明确指定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.