C# 创建类实例的快捷方式

发布于 2024-11-06 13:52:08 字数 1092 浏览 5 评论 0原文

我确信有一种方法可以轻松创建类的实例,但我在互联网上的搜索还没有找到它。假设我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黄昏下泛黄的笔记 2024-11-13 13:52:08

当您使用 new ( ) 时,您将调用与参数匹配的构造函数。如果没有定义的构造函数,您将得到一个隐式无参数构造函数。

要使用快捷方式初始值设定项,请使用类似这样的内容。

public class sCls
{
    public int A;
    public string B;

}
static void Main(string[] args)
{
   sCls oCls = new sCls() {A = 4, B = "HI"};
}

从注释中编辑


如果您添加一个采用参数的构造函数,您将丢失隐式无参数构造函数

public class sCls
{
    public sCls(string setB)
    {
        B = setB;
    }
    public int A;
    public string B;

}
static void Main(string[] args)
{
    sCls oCls = new sCls() {A = 4, B = "HI"}; // ERROR  error CS1729: 'csCA.Program.sCls' does not contain a constructor that takes 0 arguments
}

您还可以将任何构造函数与初始值设定项列表一起使用

public class sCls
{
    public sCls(string setB)
    {
        B = setB;
    }
    public int A;
    public string B;

}
static void Main(string[] args)
{
    sCls oCls = new sCls("hi") {A = 4, B = "HI"};
}

请记住,在所有情况下,即使它具有无参数构造函数,构造函数都会在初始值设定项列表之前调用。因此,基类构造或对象构造中发生的任何其他事情都将首先发生。

    public class BSE
    {
        public BSE()
        {
            BaseA = "Bob";
        }
        public string BaseA;
    }
    public class sCls :BSE
    {

        public int A;
        public string B;

    }
static void Main(string[] args)
{
        sCls oCls = new sCls() {A = 4, B = "HI" };
        Console.WriteLine("{0}", oCls.BaseA);//Prints Bob
}

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.

public class sCls
{
    public int A;
    public string B;

}
static void Main(string[] args)
{
   sCls oCls = new sCls() {A = 4, B = "HI"};
}

Edit


From comments if you add a consturctor that takes a paramter you lose the implict paramterless constructor

public class sCls
{
    public sCls(string setB)
    {
        B = setB;
    }
    public int A;
    public string B;

}
static void Main(string[] args)
{
    sCls oCls = new sCls() {A = 4, B = "HI"}; // ERROR  error CS1729: 'csCA.Program.sCls' does not contain a constructor that takes 0 arguments
}

You can also use any constructor with the initializer list

public class sCls
{
    public sCls(string setB)
    {
        B = setB;
    }
    public int A;
    public string B;

}
static void Main(string[] args)
{
    sCls oCls = new sCls("hi") {A = 4, B = "HI"};
}

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.

    public class BSE
    {
        public BSE()
        {
            BaseA = "Bob";
        }
        public string BaseA;
    }
    public class sCls :BSE
    {

        public int A;
        public string B;

    }
static void Main(string[] args)
{
        sCls oCls = new sCls() {A = 4, B = "HI" };
        Console.WriteLine("{0}", oCls.BaseA);//Prints Bob
}
鱼窥荷 2024-11-13 13:52:08

添加默认构造函数,然后尝试如下操作:

new LicencedCustomer() { LMAA_CODE = ..., LICENSE_NUMBER = ..., ... };

附注:大写属性并不常见。

Add a default constructor, then try something like:

new LicencedCustomer() { LMAA_CODE = ..., LICENSE_NUMBER = ..., ... };

Side note: It's not conventional to capitalize properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文