C # label label for instantiating a class

I'm sure there was a way to easily instantiate the class, but my search in large interdeclarations did not find it. Let's 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 is as follows.

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 a constructor right away, I get an error that the class does not contain a constructor that takes 5 arguments (initially I tried it only with the values ​​and field names in the List.Add function).

Is there a shortcut that allows you to assign properties when creating without having to explicitly define the constructor?

Thank!

EDITING. Large-scale curiosity was caused by capitalized properties - they are there only because they were created to reflect the headers of the import file. Not my preferred method!

+3
2

new() , . , less constructor.

, - .

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
}
+4

, - :

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

Side note: it is not customary to use to use properties.

+4
source

All Articles