Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607302
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:41:01+00:00 2026-05-22T00:41:01+00:00

I’m sure there was a way to easily create an instance of a class

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:41:02+00:00Added an answer on May 22, 2026 at 12:41 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.