开发者

C# Variable Naming

开发者 https://www.devze.com 2023-03-22 02:47 出处:网络
I was wondering what the best way of naming a variable is in C#? I know there are several different ways but I was just wondering why some people prefe开发者_运维问答r one over the other?

I was wondering what the best way of naming a variable is in C#? I know there are several different ways but I was just wondering why some people prefe开发者_运维问答r one over the other?

I tend to use a lowercase letter representing the type (I think this is the Hungarian method?) but a lot of people don't like this, which is the best way?

Example:

string sMyString
string MyString
string myString
string mystring

Thanks


The most common convention I have seen with c# when naming fields and local variables is camel case:

string myString

Here are the Microsoft guidelines for capitalization in c#. They state (amongst others):

The following guidelines provide the general rules for identifiers.

Do use Pascal casing for all public member, type, and namespace names consisting of multiple words.

Note that this rule does not apply to instance fields. For reasons that are detailed in the Member Design Guidelines, you should not use public instance fields.

Do use camel casing for parameter names.


I would add that this is something that needs to be agreed with your team members.


The long-emerging trend is along the lines of

string meaningfulNameOfVariable;

with camel case, and clear names that are actually meaningful to you, your context, and to other developers.


I would go for string myString, that is the normal C# way. If you look at the samples in the MSDN documentation for .NET/C# you quickly get a feeling for the best practice.

Local variables are camelCased. Hungarian notation is considered bad practice in a type safe language. The type system takes care of knowing the type for you.


I would refer to the MSDN guidelines. My practice is, at least where I've worked, string myDescriptiveStringName for local variables and private string _myDescriptiveStringName for class variables. For properties, it would be public string MyDescriptiveStringName { get; set; }

But, most organizations follow (or at least they should) certain conventions. Better not deviate from those guidelines unless you want to be in the hot seat during peer reviews. ;-) There are a few exceptions to deviate, of course.


This is from MSDN http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx

Also refer http://msdn.microsoft.com/en-us/library/ms229045.aspx


In practice, this is the matter of agreement ) Naming becomes problem when everyone in the department writes by its own vision )

Use ReSharper or similar tools with same naming-convention properties within your department )


It depends on different things... If you are coding on your own, you may use what you prefer... Normally I think most C# developers prefer camel case...

If you are working for a company etc. you have to adhere to their coding standards...

I think most important thing is that the variable names should be pretty much readable and that is the ultimate goal of better variable naming...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号