Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
meyhem2235yParameters should be camelCase
public Myclass(int myVariable) {
MyVariable = myVariable;
} -
@meyhem
But if you have the field: private int myVar; it is camelcased too so then the this.myVar or the _myVar style? -
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;
}
} -
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;
-
CptFox16165yFirst 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 -
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.
-
inaba45895y@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
-
@inaba yeah that first part is what I describe in my comment and the other way screams java to the heavens so no.
-
inaba45895y@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
-
CptFox16165y@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 ?
-
Neither, both don't conform to the guidelines and conventions which developers that develop in C# should follow.
-
@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.
-
@gitpush the underscore is not mandatory, or was that changed in the official guidelines?
-
@halfflat it is not mandatory to use camel casing, but you should if you do c#.
-
@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.
Related Rants
C# styling question: in a constructor do you prefer:
public MyClass(int MyVariable) {
this.MyVariable = MyVariable;
}
or
public MyClass(int _MyVariable) {
MyVariable = _MyVariable;
}
?
rant
style
csharp
c#