Question

I've been programming for so long its hard to keep up with language changes sometimes...

Is it really ok to set properties like this after .net v2

    public string LocaleName
    {
        get;
        set;
    }

Not requiring an inner field? Seems like the compiler takes care of this lately?

Was it helpful?

Solution

Yes, this is a new feature in C# 3.0

OTHER TIPS

It's fine as long as you don't need to do any checking to see if the values are set the right way.

You might take a look at the C# Specification.

Just so you know, you can also do something like this:

public string MyString
{
   get;
   private set;
}

which gives you a public accessor but a private setter.

Yes, these are called 'auto implemented properties'. Compiler will create a backing field for your property.

Because 'auto implemented properties' are 'C# compiler trick', you can use them in your code and target .NET framework 2.0, as long as you use C# 3.0 compiler to compile your code.

Yes, they're called automatic properties, and will generate the backing field behind the scenes.

Yes. In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property's get and set accessors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top