Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET3.5中的新构造函数_C#_.net - Fatal编程技术网

C# .NET3.5中的新构造函数

C# .NET3.5中的新构造函数,c#,.net,C#,.net,这真是一个简单的问题。我一直在使用.NET3.5(C#)中的新型构造函数,但如果它们有名称的话,我想知道它们的名称:) 我说的构造函数是: Customer c = new Customer() { Name = "Bo" }; 您使用的是常规的无参数构造函数,还有一个新功能,称为。它们称为对象初始值设定项。关于它们的更多信息可以在这里找到:正如其他人已经指出的,它们被称为对象初始值设定项 但是,他们是而不是构造函数,您不应该到处提及他们 考虑以下代码: public class Te

这真是一个简单的问题。我一直在使用.NET3.5(C#)中的新型构造函数,但如果它们有名称的话,我想知道它们的名称:)

我说的构造函数是:

Customer c = new Customer()
{
    Name = "Bo"
};

您使用的是常规的无参数构造函数,还有一个新功能,称为。它们称为对象初始值设定项。关于它们的更多信息可以在这里找到:

正如其他人已经指出的,它们被称为对象初始值设定项

但是,他们是而不是构造函数,您不应该到处提及他们

考虑以下代码:

public class TestHarness
{
    static void Main(string[] args)
    {
        Class1 class1 = new Class1();
        class1.Foo = "foo";

        Class2 class2 =
            new Class2
            {
                Foo = "foo"
            };
    }
}

public class Class1
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

public class Class2
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}
查看为主要方法生成的IL:

.method private hidebysig static void Main(string[] args) cil managed
{
    .maxstack 2
    .locals init (
        [0] class ClassLibrary1.Class1 class2,
        [1] class ClassLibrary1.Class2 class3,
        [2] class ClassLibrary1.Class2 class4)
    L_0000: nop 
    L_0001: newobj instance void ClassLibrary1.Class1::.ctor()
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: ldstr "foo"
    L_000d: callvirt instance void ClassLibrary1.Class1::set_Foo(string)
    L_0012: nop 
    L_0013: newobj instance void ClassLibrary1.Class2::.ctor()
    L_0018: stloc.2 
    L_0019: ldloc.2 
    L_001a: ldstr "foo"
    L_001f: callvirt instance void ClassLibrary1.Class2::set_Foo(string)
    L_0024: nop 
    L_0025: ldloc.2 
    L_0026: stloc.1 
    L_0027: ret 
}

您可以看到编译器生成了为class1和class2设置Foo属性的代码。它没有生成接受并设置Foo的构造函数。一个小问题,但最好能理解其中的区别。

该死,连链接都比我强:)@Richard Szalay:不,我没有,Martinho有。干得好,马丁尼奥@博莫滕森:没问题。总是乐于帮助:)注意,在这种情况下,您可以省略以下选项:
newcustomer{Name=“Bo”}您应该在任何代码之前放置四个空格,以便将其正确格式化为代码。这是为你做的。:)好的,非常感谢Martinho:)