loading...

Naming conventions in c#

davidolawale profile image David Olawale Updated on ・2 min read

Here is the most common naming conventions in C#

lets first talk about different kinds of cases

Pascal Case

Pascal case variables begin with capital letter, first letter of every word is also capital eg Account, FirstName, SizeInBytes

Camel Case

Camel case variables begin with small letter, first letter of every subsequent word is capital eg account, firstName, sizeInBytes

Snake Case

Snake case variables have small letters all through, worlds are seperated with an underscore eg account, first_name, size_in_bytes

Flat Case

flat case variables have small letters all through, no delimiter for words eg account, firstname, sizeinbytes

There are many more cases but these are the common ones
lets now see how to name in c#

Class

class names should be pascal, eg:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Methods

Method names should be camel, eg:

    public void DoSomething()
    {
        // do stuff here
    }

Method Parameters

Method parameter names should be cascal case, eg:

public int Add(int firstNumber, int secondNumber)
{
    return firstNumber + secondNumber;
}

Class Fields

Public fields should be camel while private fields can be pascal or pascal case preceeded with an underscore , eg:

public class User
    {
        public string Name;
        private string userId;
    }
    class Product
    {
        public string Name;
        private string _serialNumber;
        public DateTime CreationDate;
    }

However you should be consistent, don't prepend an underscore to a private field in one class and not do so in another class

Local variables

local parameter names should be cascal case, eg:

int result = Add(12, 23);

Implicit vs Explicit typing

You should explicitly = specify the type of variable if

  • The variable is not initialized with a constructor
  • The type of the variable is not obvious You may allow the compiler to infer the type of variables (implicit) if
  • The type of the variable is obvious -- The variable is an object literal

examples

// ok because it's obvious that the type of this variable is User
var user = new User();

// bad because the type of the returned result is not obvious
var result = Processor.DoSomeWork();

// ok
WorkResult result = Processor.DoSomeWork();

// ok because name is a string literal
var name = "John Paul";

// ok because result is an integer literal
var result = 200

Posted on May 24 by:

davidolawale profile

David Olawale

@davidolawale

A two years experienced web developer focussed on c# and javascript

Discussion

markdown guide
 

There is a mixup between the naming convention you suggest with the example code thats written. "class names should be camel" but then the code is written with Pascal case.

No intent to flame openly, if I knew how to send a DM, I would (please enlighten me if you know how :))

 

Thanks for notifying me, class names should be Pascal case, that was a mistake, i'll correct it immediately