3

The normal usage for a property in C# is :

private int aVar;
public int AVar { get; set; }

or just

public int AVar { get; set; }

But for reason of clarity I prefer to name private attribute with an _ before :

private int _aVar;

and then a property :

public int AVar
{
    get
    {
        return (_aVar);
    }
    set
    {
        _aVar = value;
    }
}

Is there a way to do some kind of shortcut to have something like :

private int _aVar;
public int AVar { get; set; } : link _aVar

?

(Other than : "Use the visual studio snippet you lazy bum.", or "In C# everybody uses aVar and a AVar so don't use your stupid _ and everything will be fine." if possible)

ashtrail
  • 93
  • 2
  • 9
  • 2
    I don't know what you mean by the first example, but that `aVar` doesn't do anything. And no, you can't do the latter. What problem are you trying to solve? – CodeCaster Sep 29 '15 at 09:11
  • the stupid '_' is banish with StyleCop rule SA1309 ;) http://stylecop.soyuz5.com/SA1309.html – Xaruth Sep 29 '15 at 09:13
  • 3
    @Xaruth [SA1309 is silly](http://stackoverflow.com/questions/3215395/ca1500-vs-sa1309-which-one-wins), "everyone" uses the underscore prefix for private members. – CodeCaster Sep 29 '15 at 09:14
  • Not sure I understand why `(_aVar)` has parentheses. –  Sep 29 '15 at 09:15
  • @CodeCaster "everyone" who wrote in C++ before ;) – Xaruth Sep 29 '15 at 09:15
  • 2
    Could you elaborate on why you think `private int _aVar; public int AVar { get; set; } : link _aVar` is more clear than just `public int AVar { get; set; }`? – Corak Sep 29 '15 at 09:15
  • For people new to C#. And say you have a big object with method that call other attributes it goes much quicker to see what's private and what isn't I find. But I can still survive without _ it's more curiosity than a need. – ashtrail Sep 29 '15 at 09:18
  • 3
    If you don't want to do anything in the get;set; then just keep it AVar {get;set;} and Remove _aVar; It is completely pointless to link _aVar to AVar if you don't want to do some sort of work in the get;set; – Donald Jansen Sep 29 '15 at 09:19
  • Yes Indeed... Didn't cross my mind. – ashtrail Sep 29 '15 at 09:21
  • 1
    @JayMee : "return (a_Var)" -> My school coding convention habit ^^ – ashtrail Sep 29 '15 at 09:29

2 Answers2

4

In short, no.

I assume you're aware of the visual studio snippet propfull [tab] [tab]. This will create both a 'typical' member and property syntax for you.

  • OK thanks then, as I said in the comments this was more curiosity than anything else. – ashtrail Sep 29 '15 at 09:19
  • @ashtrail - nothing wrong with curiosity. It's good to experiment with what C# can and can't do, not a bad question in my opinion. –  Sep 29 '15 at 09:24
2

In C#6 there's a shorthand for this for read-only properties, but not for writeable properties.

Instead of

public int[] AVar
{
    get
    {
        return (_aVar);
    }
}

you can put

public int[] AVar => _aVar;

This only works for READ ONLY properties though. (You should not normally make an array property writable, by the way.)

Is that the sort of shortcut you were looking for?

However, note that for your specific example, you might as well just use an auto-property:

public int[] AVar { get; set; }
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276