4

C# styling question: in a constructor do you prefer:

public MyClass(int MyVariable) {
this.MyVariable = MyVariable;
}

or

public MyClass(int _MyVariable) {
MyVariable = _MyVariable;
}

?

Comments
  • 18
    Parameters should be camelCase

    public Myclass(int myVariable) {

    MyVariable = myVariable;

    }
  • 1
    @meyhem
    But if you have the field: private int myVar; it is camelcased too so then the this.myVar or the _myVar style?
  • 4
    Both look really ugly. I much prefer this https://dotnetfiddle.net/X8LC1X
  • 5
    According to Microsoft, it should be camelCase for all params passed to methods/constructors...etc

    For fields it is: _myVar
    For properties: MyVar

    Edit adding example:

    class Test {
    public string _myVar;
    public string MyVar {get;set;}

    public Test(string newVar){
    this._myVar = newVar;
    this.MyVar = newVar;
    }

    }
  • 3
    I won't put that ugly underscore
  • 2
    The thing is I like to use the constructor parameters for things that HAVE to be the there, and the object initialization for optional, mostly pure data stuff. I hate the underscore too @Lyniven and i strongly prefer the this.Variable syntax, you instantly know ok this is gonna be this field of this class. If any processing happens in the constructor i change the name of the variable to something descriptive about the thing thats gonna happen to it, e.g. string passwordHashInput;
  • 1
    2nd one
  • 1
    C# is ugly anyways. But first one is better
  • 3
    First one.

    In general, I dislike using "variable" as a shortcut to "this.variable", it doesn't save much as far as verbosity goes, but makes the mutation of "this" less clear
  • 1
    First one, without a doubt. I'll always prefer the explicit case that's always correct than trusting that made up naming conventions are respected.
  • 2
    Use what @inaba said
  • 1
    personally i prefer this.foo = foo
  • 0
    @EdoPhoenix Then do ```public MyClass(int myVariable) { MyVariable = myVariable; }``` or have MyClass take a MyClassDataObject (which is what a lot of other libraries do) and check for the things you insert
  • 0
    @inaba yeah that first part is what I describe in my comment and the other way screams java to the heavens so no.
  • 0
    @EdoPhoenix No in the first part you used pascal case for both parameter and property and in my example I use camel case for the parameter and pascal case for the property. This also allows me to not call _this_ which is especially nice
  • 1
    @inaba How is not using "this" nice ? That way the fact that you refer to the object is implicit rather than explicit, but you save 5 characters so I guess it's worth it ?
  • 0
    First one FTW
  • 1
    Neither, both don't conform to the guidelines and conventions which developers that develop in C# should follow.
  • 1
    @inaba object initializer is not the same as a constructor. I hope you do use constructors for those dependencies and properties that are required for your class to work properly. Although object initializer is more expressive constructors do have their purpose and they keep code much more concise.
  • 0
    @gitpush the underscore is not mandatory, or was that changed in the official guidelines?
  • 0
    @halfflat it is not mandatory to use camel casing, but you should if you do c#.
  • 1
    @soggybutter made up? By who? If every c# dev would have the discipline to follow the official guidelines then we wouldn't have this discussion.
Add Comment