Jump to content
  • Advertisement
Sign in to follow this  
janta

C# programming style (type name vs. var name)

This topic is 3613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I often see variables with the same name as their type; is that considered bad programming style in C#? Ex:
class SomeClass
{
    public Size Size { get; set }
}
So far my compiler seems to do a perfect job figuring out which are types or variables, and the code does not seem confusing to me. What's the take of experienced programmers on that question?

Share this post


Link to post
Share on other sites
Advertisement
I'm not a very experienced programmer, but I think that it depends on what you prefer.. I never name my variables identically to the types, but if you dont have a problem with distiguishing between them then why change it?

I always have variable in lower-case only and types are Uppercase (the first letter only though)

Share this post


Link to post
Share on other sites
It's pretty common naming scheme in .NET. If the property just represents size then why bother inventing any other name. Compiler doesn't have any problems with it and moreover Microsoft encourages programmers to do it.

Share this post


Link to post
Share on other sites
In c++ it wouldn't be possible, but there a pretty common scheme is to use Classname classname to declare a variable classname of type Classname (and if you have a vector/array/similar, just add an s, pluralizing it, like in "Classname classnames"). With the same reasoning as above.

Share this post


Link to post
Share on other sites
I was surprised to find that C# let me name variables the same as classes. It does seem odd, but it works and seems to be accepted style.

While it doesn't address this issue in particular, I found the following naming convention site helpful.

http://www.irritatedvowel.com/Programming/Standards.aspx

Share this post


Link to post
Share on other sites
I assume its good style since it gets used a number of times in microsoft's .net librarys

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!