views:

778

answers:

8

I started working with C# recently and I noticed that the convention seems to be that the variables start with a capital letter along with the methods.

Is this the only language that does this and why? For instance:

Page Page = new Page();
Page.Action();

In other languages, you'd see instead:

Page page = new Page();
page.action();

There are other examples of this that are confusing since I've worked a lot with UML, Ruby, C++ and Java.

My question is, why does C# do it this way when other languages do not?

Edit

Other Stack Overflow users are noting that C# does not follow this convention, this was just a mistake on my part.

+17  A: 

Well actually, no: the convention in C# is for camelCased variable (and field) names, and PascalCase methods:

Page page = new Page();
page.Action();
Marc Gravell
PascalCase is for public methods, properties, and events
Michael Meadows
Ok, I guess the code I saw so far was just messy, then. Thanks
marcgg
+2  A: 

Every set of C# conventions I've written or used would specify camel case for variables, so:

Page page = new Page();
MyClass myClass = new MyClass();

Not sure where you've seen Pascal case used, but it's certainly not something inherent to, or standard for, C#.

David M
+2  A: 

Variables in C# typically don't start with an uppercase letter (though that's obviously up to the developer's own implementation). What you're probably confused about is the concept of a Property. Properties in C# are used syntactically like variables (in that they can be retrieved or assigned, rather than just executed like a function), but can encapsulate business logic on both operations.

To answer a broader part of your question, though, both properties and methods/functions do typically start with an uppercase letter according to the Microsoft guidelines.

Adam Robinson
+10  A: 

No, this is fairly non-standard C# code. The .Net Framework Design guidelines in section 2.6 recomend the use of camel casing for local variable names.

JaredPar
This is a very good article about why C# is the way it is. I personally love the standard he list here. At my job they have a horrific naming convention of l_s_SomeString for a local string. I hate underscores.
David Basarab
+4  A: 

Public members use PascalCase, private members use camelCase.

I think that this makes it clearer which methods support a class versus which methods define a class.

public class Foo
{
    private Bar bar;
    public Bar Bar { get; set; }

    public void DoFoo()
    {
        makeFoo();
    }

    private void makeFoo()
    {
    }
}
Michael Meadows
According to the Microsoft Naming Guidelines makeFoo() should be named MakeFoo(). There is no difference in the casing for methods, properties, and events depending on their scope. http://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspx
Daniel Brückner
You're right. The published guidelines only concern themselves with publicly exposed members, and leave internal guidelines to the implementer. In C#, I have only ever seen camelCase for private methods, but I suppose it's not a "standard" per se. Like all other conventions for private members, it's up to the implementer to define. Thanks for the clarification.
Michael Meadows
+1  A: 

PascalCase was a convention at Microsoft long before .NET. (In the Win32 API etc.)

A convention also makes sense to use within a single environment. .NET being a comprehensive environment on its own, and Microsoft-the-company another, there's really no point to adopting someone else's.

Also, I strongly doubt UML has a naming convention or that even the idea of UML having a naming convention makes sense. UML models your software, and should follow the convention of that software.

Sii
+2  A: 

Public = PascalCase, I only tend to use camelCase for private fields, and parameter arguments.

Read this coding standard though easily searchable on google, not a bad place to start.

Mark Dickinson
This is helpful, thanks
marcgg
+2  A: 

You can find out a lot about what conventions should be adopted by using the two following tools.

FxCop: http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx

StyleCop: http://code.msdn.microsoft.com/sourceanalysis

Joshua Belden