6

In C# and its cousin languages, we always use

public string SomeString { get; set;}

But you can also use ( I found this out only recently and while fooling around with the compiler )

public string SomeString { set; get; }

I do not have any formal training in programming and everything is self-tought. I have been using { get; set; } without any thought just like we use 1 + 1 = 2 Is the order of { get; set; } just a convention or is it necessary to maintain this order or is it some remnant of a bygone era of C history like the way we define conventional electric current flowing from positive to the negative terminal when it is actually the other way around?

5
  • Fix your second code sample; you've skipped the variable name. Commented Oct 14, 2013 at 11:29
  • 2
    @Dariusz Wouldn't it have been easier to edit the question, rather than post a comment requesting the OP edit it? Commented Oct 14, 2013 at 11:31
  • 3
    @DavidArno the community frowns upon editing code in questions. Search the meta. While this is a fairly obvious case, I wasn't 101% certain it wasn't an error. Commented Oct 14, 2013 at 11:32
  • @Dariusz, Thanks, I didn't know that. I've been naively editing bad indentation and obvious typos unrelated to the question. I'd best stop though if that's the wrong thing to do. Commented Oct 14, 2013 at 11:34
  • @DavidArno I don't say I think what you did is wrong. I just don't like to take chances. If it was an answer, I would've edited the code too. Commented Oct 14, 2013 at 11:35

7 Answers 7

9

It is purely a convention. It makes no difference which order they appear in.

Sign up to request clarification or add additional context in comments.

3 Comments

This is on par with what order you overload ToString() or GetHashCode(). Property accessors are just shortcuts to get_varname/set_varname methods.
Be careful with the "are just shortcuts to" claim. I can't write a method get_X() and then reference it as X. The concept of properties exists in the CLR and thus X becomes get_X() with "I'm a property" metadata.
My apologies. I wasnt inferring there's a direct correlation, just insight in to behind the scenes.
5

There is no difference.

It is exactly as if you had implemented the getter first in your class body, and the setter after it. The functions would still do exactly the same:

public String getSomeString() { return someString; }
public void setSomeString(String value) { someString=value; }

Whether they are written in that order

public void setSomeString(String value) { someString=value; }
public String getSomeString() { return someString; }

or the opposite. Wouldn't they?

I would however suggest to stick to one order in your code. Less entropy is always better :)

Comments

2

There is no difference.

According to the C# Language Specification http://msdn.microsoft.com/en-us/library/ms228593.aspx, 10.7.2 Accessors (page 324)

The accessor-declarations of a property specify the executable statements associated with reading and writing that property.

accessor-declarations:

get-accessor-declaration   set-accessor-declaration
set-accessor-declaration   get-accessor-declaration

As shown it states either order has the same effect

Comments

2

Internally Get and Set are methods like this

private PropertyType Get() {}
private Set(value as PropertyType) {} 

Since order of declaration of methods is not important, same case goes here.

MSDN:

The body of the get accessor is similar to that of a method. It must return a value of the property type.


The set accessor is similar to a method that returns void. It uses an implicit parameter called value, whose type is the type of the property.

Comments

1

{ get; set; } is just a shortcut so you don't have to write getters and setters for every field you want to expose. It's the same as when you write

public string GetSomeString() { }
public void SetSomeString(string value) { } 

Does it matter, which one you write first? Of course not.

Comments

1

Just a convention you can use any of these when defining parameters:

public string SomeString { get; set; }
public string SomeString2 { set; get; }
    
public string someString2;

public string SomeString21
{
    get { return someString2; }
    set { someString2 = value; }
}

public string SomeString22
{
    set { someString2 = value; }
    get { return someString2; }
}

public string SomeString23
{
    set { someString2 = value; }
}

public string SomeString24
{
    get { return someString2; }
}

Comments

0

As others have already pointed out, there is no difference and it is just a convention. But to prove that up, you can see how compiler actually treats your code, given the following:

public class C 
{
    public string SomeString { get; set;}
    public string SomeString2 { set; get; }
}

This will be treated as:

public class C
{
    [CompilerGenerated]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string <SomeString>k__BackingField;

    [CompilerGenerated]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string <SomeString2>k__BackingField;

    public string SomeString
    {
        [CompilerGenerated]
        get
        {
            return <SomeString>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            <SomeString>k__BackingField = value;
        }
    }

    public string SomeString2
    {
        [CompilerGenerated]
        get
        {
            return <SomeString2>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            <SomeString2>k__BackingField = value;
        }
    }
}

As you can see, in both of them a new BackingField is generated by compiler and the body of two properties are same exactly.

The reference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.