2

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?

ripper234
  • 222,824
  • 274
  • 634
  • 905
JL.
  • 78,954
  • 126
  • 311
  • 459
  • You might change your question to reflect that your asking which version of C# you have installed. – Kredns Jul 22 '09 at 20:01
  • that is correct and you can limit the accesiobility of each if needed E.g. public string LocaleNam { get; private set;} – Rune FS Jul 22 '09 at 20:05

6 Answers6

10

Yes, this is a new feature in C# 3.0

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • So I amn programming in C# v3, but using the .net v2 framework? – JL. Jul 22 '09 at 19:53
  • 1
    @JL, yes, it's a *C#* feature that doesn't require any specific framework version. :) – Sam Harwell Jul 22 '09 at 19:54
  • 2
    It's more of a compiler feature with Visual Studio 2008. If you're using VS2005 with .NET 3.0 extensions, you would not get this feature. – Will Eddins Jul 22 '09 at 19:56
  • @280Z28 A C# feature may or may not need CLR support e.g. LINQ to 'X' does need CLR support whereas auto implemented properties don't because they are purely a compiler trick. – SolutionYogi Jul 22 '09 at 19:56
  • This feature requires the C# 3 compiler. – Andrew Hare Jul 22 '09 at 19:57
  • Cool, thanks Andrew - good point, its all about how it gets compiled – JL. Jul 22 '09 at 19:59
  • @solutionYogi LINQ does require the same version of the CLR as auto properties (v2) it's syntactic sugar in both cases. There was no change to the CLR going from c#v2 to C#v3 – Rune FS Jul 22 '09 at 20:03
  • The term for this is syntactic sugar - the compiler is just making the syntax easier but the generated IL is actually still using an inner field. – Robert MacLean Jul 22 '09 at 20:08
3

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.

Kredns
  • 36,461
  • 52
  • 152
  • 203
3

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.

Sterno
  • 1,638
  • 2
  • 17
  • 28
1

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.

SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
0

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

Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
0

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.