Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 6 auto-properties - read once or every time?

I follow a pattern when setting certain properties whereby I check to see if the corresponding field is empty, returning the field if not and setting it if so. I frequently use this for reading configuration settings, for example, so that the setting is read lazily and so that it is only read once. Here is an example:

private string DatabaseId
{
    get
    {
        if (string.IsNullOrEmpty(databaseId))
        {
            databaseId = CloudConfigurationManager.GetSetting("database");
        }

        return databaseId;
    }
}

I have started to use C# 6 autoproperty initialization as it really cleans up and makes my code more concise. I would like to do something like this:

private string DatabaseId { get; } = CloudConfigurationManager.GetSetting("database");

But I'm not sure how the compiler interprets it in this case. Will this have the same effect as my first block of code, setting the (automatically implemented) field once, and thereafter reading from the field? Or will this call the CloudConfigurationManager every time I get DatabaseId?

like image 687
08Dc91wk Avatar asked Aug 17 '15 13:08

08Dc91wk


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


3 Answers

What you show:

private string DatabaseId { get; } = CloudConfigurationManager.GetSetting("database");

Is an "Auto-Property Initializer", keyword being "initializer", from MSDN Blogs: C# : The New and Improved C# 6.0:

The auto-property initializer allows assignment of properties directly within their declaration. For read-only properties, it takes care of all the ceremony required to ensure the property is immutable.

Initializers run once per instance (or once per type for static members). See C# Language Specification, 10.4.5 Variable initializers:

For instance fields, variable initializers correspond to assignment statements that are executed when an instance of the class is created.

So that code compiles to something like this:

public class ContainingClass
{
    private readonly string _databaseId;
    public string DatabaseId { get { return _databaseId; } }

    public ContainingClass()
    {
        _databaseId = CloudConfigurationManager.GetSetting("database");
    }       
}

For static variables, this kind of looks the same:

private static string DatabaseId { get; } = CloudConfigurationManager.GetSetting("database");

Compiles to, more or less:

public class ContainingClass
{
    private static readonly string _databaseId;
    public static string DatabaseId { get { return _databaseId; } }

    static ContainingClass()
    {
        _databaseId = CloudConfigurationManager.GetSetting("database");
    }       
}

Though not entirely, as when the type doesn't have a static constructor, "static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class".

like image 157
CodeCaster Avatar answered Oct 22 '22 22:10

CodeCaster


C# 6.0 readonly auto property will create a field and invoke the initializer only once.

However, that is not equal to what you have there. In your code, CloudConfigurationManager.GetSetting will be called only when someone reads the DatabaseId property but with "readonly auto property" CloudConfigurationManager.GetSetting will be called at the time of class initialization itself.

This difference may/mayn't matter. It depends. If the call is expensive then you can use Lazy<T> which is roughly equal to what you have.

like image 44
Sriram Sakthivel Avatar answered Oct 22 '22 22:10

Sriram Sakthivel


It will set the value only once and after that just read it.

However there's a slight difference in the sense that you now no longer have a databaseId field. In your first example you basically check for id == null || id == "" to set the database string. That means that if you create a new instance with databaseId set to an empty string, the first example will still get the ID from the settings.

The second example however will see that empty string as a valid value and remain with it.

First code:

if(id == null || id == "") // Get ID from settings

Second code:

if(id == null) // Get ID from settings
like image 2
Jeroen Vannevel Avatar answered Oct 23 '22 00:10

Jeroen Vannevel