Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Initialising member variables to be exposed via properties

Tags:

c#

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.

like image 953
CSharpened Avatar asked Nov 30 '25 04:11

CSharpened


2 Answers

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.)

like image 185
Jon Skeet Avatar answered Dec 01 '25 18:12

Jon Skeet


You are essentially doing the same thing but writing it in a different form.

I always prefer (and use) the second aprroach because I don't like methods being executed in the middle of nowhere. It's better to split things. Attributes are declared on class body and initialized on class constructor.

like image 25
Ignacio Soler Garcia Avatar answered Dec 01 '25 20:12

Ignacio Soler Garcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!