C# 创建类实例的快捷方式
我确信有一种方法可以轻松创建类的实例,但我在互联网上的搜索还没有找到它。假设我有这个:
List<LicencedCustomer> leftList = new List<LicencedCustomer>();
leftList.Add(new LicencedCustomer (LMAA_CODE:"1",LICENSE_NUMBER:"1",TRADING_NAME:"Bobs Liquor",STATE:"NSW",POSTCODE:"2261"));
我的课程如下所示。
public class LicencedCustomer
{
public string LMAA_CODE {get; set;}
public string LICENSE_NUMBER {get; set;}
public string TRADING_NAME {get; set;}
public string STATE {get; set;}
public string POSTCODE {get; set;}
public LicencedCustomer(string LMAA_CODE, string LICENSE_NUMBER, string TRADING_NAME, string STATE, string POSTCODE)
{
this.LMAA_CODE = LMAA_CODE;
this.LICENSE_NUMBER = LICENSE_NUMBER;
this.TRADING_NAME = TRADING_NAME;
this.STATE = STATE;
this.POSTCODE = POSTCODE;
}
...
如果没有上面的构造函数,我会收到一个错误,该类不包含带有 5 个参数的构造函数(最初我尝试仅使用值,而 List.Add 函数中没有字段名称)。
是否有一种快捷方式允许在创建时分配属性,而无需显式定义构造函数?
谢谢!
编辑:广泛的好奇心源于大写的属性 - 它们之所以如此,是因为它们是为了反映导入文件的标题而构建的。这不是我喜欢的方法!
I'm sure there was a way to easily create an instance of a class but my search of the great interwebs hasn't found it. Lets say I have this:
List<LicencedCustomer> leftList = new List<LicencedCustomer>();
leftList.Add(new LicencedCustomer (LMAA_CODE:"1",LICENSE_NUMBER:"1",TRADING_NAME:"Bobs Liquor",STATE:"NSW",POSTCODE:"2261"));
My class looks like the below.
public class LicencedCustomer
{
public string LMAA_CODE {get; set;}
public string LICENSE_NUMBER {get; set;}
public string TRADING_NAME {get; set;}
public string STATE {get; set;}
public string POSTCODE {get; set;}
public LicencedCustomer(string LMAA_CODE, string LICENSE_NUMBER, string TRADING_NAME, string STATE, string POSTCODE)
{
this.LMAA_CODE = LMAA_CODE;
this.LICENSE_NUMBER = LICENSE_NUMBER;
this.TRADING_NAME = TRADING_NAME;
this.STATE = STATE;
this.POSTCODE = POSTCODE;
}
...
Without the constructor immediately above, I get an error that the class doesn't contain a constructor that takes 5 arguments (initially I tried it with the values only and no field names in the List.Add function).
Is there a shortcut that allows assignment to properties on creation, without needing to define the constructor explicitly?
Thanks!
EDIT: Wide ranging curiosity has resulted from the capitalised properties - they are only that way because they've been built to reflect the headings of an import file. Not my preferred method!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用 new ( ) 时,您将调用与参数匹配的构造函数。如果没有定义的构造函数,您将得到一个隐式无参数构造函数。
要使用快捷方式初始值设定项,请使用类似这样的内容。
从注释中编辑
如果您添加一个采用参数的构造函数,您将丢失隐式无参数构造函数
您还可以将任何构造函数与初始值设定项列表一起使用
请记住,在所有情况下,即使它具有无参数构造函数,构造函数都会在初始值设定项列表之前调用。因此,基类构造或对象构造中发生的任何其他事情都将首先发生。
When you use new ( ) you call a constructor that matches the parameters. If you have no defined constructors you will get an implicit parameter less constructor.
To use the shortcut initializer use something like this.
Edit
From comments if you add a consturctor that takes a paramter you lose the implict paramterless constructor
You can also use any constructor with the initializer list
Remember that in all cases the constructor is called before the initializer list even if it has a parameter less constructor. So base class constructions or anything else that happens in the construction of the object will happen first.
添加默认构造函数,然后尝试如下操作:
附注:大写属性并不常见。
Add a default constructor, then try something like:
Side note: It's not conventional to capitalize properties.