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

  • Home
  • SEARCH
  • 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 79287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:07:20+00:00 2026-05-10T21:07:20+00:00

Is it better to initialize class member variables on declaration private List<Thing> _things =

  • 0

Is it better to initialize class member variables on declaration

private List<Thing> _things = new List<Thing>(); private int _arb = 99; 

or in the default constructor?

private List<Thing> _things; private int _arb;  public TheClass() {   _things = new List<Thing>();   _arb = 99; } 

Is it simply a matter of style or are there performance trade-offs, one way or the other?

  • 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. 2026-05-10T21:07:20+00:00Added an answer on May 10, 2026 at 9:07 pm

    In terms of performance, there is no real difference; field initializers are implemented as constructor logic. The only difference is that field initializers happen before any ‘base’/’this’ constructor.

    The constructor approach can be used with auto-implemented properties (field initializers cannot) – i.e.

    [DefaultValue('')] public string Foo {get;set;} public Bar() { // ctor   Foo = ''; } 

    Other than that, I tend to prefer the field initializer syntax; I find it keeps things localized – i.e.

    private readonly List<SomeClass> items = new List<SomeClass>(); public List<SomeClass> Items {get {return items;}} 

    I don’t have to go hunting up and down to find where it is assigned…

    The obvious exception is where you need to perform complex logic or deal with constructor parameters – in which case constructor-based initialization is the way to go. Likewise, if you have multiple constructors, it would be preferable for the fields to always get set the same way – so you might have ctors like:

    public Bar() : this('') {} public Bar(string foo) {Foo = foo;} 

    edit: as a side comment, note that in the above, if there are other fields (not shown) with field initializers, then they are only directly initialized in the constructors that call base(...) – i.e. the public Bar(string foo) ctor. The other constructor does not run field initializers, since it knows they are done by the this(...) ctor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This could be a possible way to do it: Store… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer You use Ref when you pass an initialized parameter and… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer I don't know of any currently available, but at the… May 11, 2026 at 8:53 pm

Related Questions

At my old C++ job, we always took great care in encapsulating member variables,
I come from the land of Java, C#, etc. I am working on a
This is related to some other questions, such as: this , and some of
When creating an MDI Application with Visual Studio style using the AppWizard of VS2008

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.