gpt4 book ai didi

c# - C# 中的构造函数和初始化器

转载 作者:太空狗 更新时间:2023-10-29 19:49:29 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What's the difference between an object initializer and a constructor?

在 C# 中,您可以构造一个对象,如:

public class MyObject{

int val1;
int val2;

public MyObject(int val1, val2){
this.val1 = val1;
this.val2 = val2;
}
}

与:

MyObject ob = new MyObject(1,2);

或与:

MyObject ob = new MyObject(){ val1 = 1, val2 = 2 };

那种构造函数有什么区别?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com