Question

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.

Was it helpful?

Solution

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

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

OTHER TIPS

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.

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()
    {
    }
}

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#.

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top