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 8141949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:40:36+00:00 2026-06-06T12:40:36+00:00

I have just written a small piece of code and it struck me that

  • 0

I have just written a small piece of code and it struck me that I am not sure what method of initialisation is best practice when it comes to initialising my member variables which will be exposed and used via properties. Which is the best way to initialise my member variables out of the two examples below and more importantly why?

Example 1:

    private string m_connectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString;
    private string m_providerName = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ProviderName;

    public string ConnectionString
    {
        get { return m_connectionString; }
        set { m_connectionString = value; }
    }

    public string ProviderName
    {
        get { return m_providerName; }
        set { m_providerName = value; }
    }

    public EntityClusterRefreshServiceDatabaseWorker()
    {
    }

Example 2:

    private string m_connectionString;
    private string m_providerName;

    public string ConnectionString
    {
        get { return m_connectionString; }
        set { m_connectionString = value; }
    }

    public string ProviderName
    {
        get { return m_providerName; }
        set { m_providerName = value; }
    }

    public EntityClusterRefreshServiceDatabaseWorker()
    {
        ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString;
        ProviderName = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ProviderName;
    }

NOTE: Assume that I am not using these variables in a static context.

  • 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-06-06T12:40:37+00:00Added an answer on June 6, 2026 at 12:40 pm

    It really doesn’t matter which of those you use, except in the very odd situation where a base class constructor calls an overridden member, in which case the timing would change: instance variable initializers are run before the base class constructor call, whereas obviously the constructor body is executed afterwards.

    In the latter case, you can make your code a lot simpler though using automatically implemented properties:

    public string ConnectionString { get; set; }
    public string ProviderName { get; set; }
    
    public EntityClusterRefreshServiceDatabaseWorker()
    {
        // Code as before
        ConnectionString = ...;
        ProviderName = ...;
    }
    

    You can’t do this with the first form, as automatically implemented properties don’t have any way of specifying an initial value.

    (You may also want to consider making the setters private, but that’s a separate concern.)

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

Sidebar

Related Questions

I have written a small piece of code to calculate quadratic equations, but if
I have a small I have just written which uses the following uri template.
I have written a small program just like hello the world , everything was
I have written a following code to get just the file name without extension
I have written a SQL query that works just fine, but am having a
I have written a function that positions a tooltip just above a textbox. The
I have a method somewhat similar to the one written bellow(this is just a
I have just written a small utility to be used in a pre-build event.
I am just having a quick look at UIWebView and have written a small
Hi all i have written a small code to open a particular text file.

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.