Skip to main content

C# Naming Standard

← Back to Home

In this Article:

πŸ€” Notice something is missing or not working? β†’ Please contact Zoel (zoelbastianbach@agate.id) on Ms. Teams!

https://embed.notionlytics.com/s/VjIxdGJWWmtiRzAzU1c1VVVsbERZMFJ1TURFPQ==

Overview


Consistency is the key to maintainable code. This statement is most true for naming your projects, source files, and identifiers including Fields, Variables, Properties, Methods, Parameters, Classes, Interfaces, and Namespaces.

  • ❓ Why should I care about this article?

    Naming conventions result in improvements in terms of "four Cs": communication, code integration, consistency and clarity. The idea is that "the code should explain itself". Following a consistent set of naming conventions in the development can be a major contribution to its usability.

  • 🎯 The goal of this article

    This article will provide a consistent set of naming conventions utilized in Agate

  • 🀷🏻 If I have further question about this article, who can I reach out to?

    Please contact: Fachri Akbar (fachri@agate.id)

The table below is the summary/TL;DR version of the naming conventions in this article:

[Naming Overview](Naming Standard e8d0afdf61254076932a7230c967b92c/Naming Overview 30543ddc7b664078948c460709d767ca.csv)

General Naming Convention


  • βœ… ALWAYS use camelCase or PascalCase names

  • ❌ AVOID the use of ALL CAPS and all lowercase names. Single lowercase words or letters are acceptable.

  • ❌ DO NOT create declarations of the same type (namespace, class, method, property, field, or parameter) and access modifier (protected, public, private, internal) that vary only by capitalization. Assume you will be working with languages that does not support case-sensitivity, even if you know you're not.

    public int SomeIntVariable;
    public int SomeintVariable;
    public int SomeIntvariable;
    
  • ❌ DO NOT use names that begin with a numeric character.

    // **❌** DO NOT do this:
    public class 6AxisControler {}
    
    // **βœ…** DO this instead:
    public class SixAxisControler {}
    
  • βœ… ALWAYS choose meaningful, self-explanation and specific names

    // **❌** DO NOT do this:
    public int Asdf;
    
    //a = final position
    //b = current position
    //c = pivot
    a = b – c;
    
    // **βœ…** DO this instead:
    public int BallCount;
    
    finalPos = currentPos – pivot;
    
  • Variables and Properties should describe an entity not the type or size. And also do not use Hungarian Notation

    // **❌** DO NOT do this:
    public int nBall; //number of ball
    public bool bBusy; //Boolean variable
    
    // **βœ…** DO this instead:
    public int BallCount;
    public bool IsBusy;
    
  • ❌ AVOID using abbreviations unless the full name is excessive (more than 25 character).

    // **❌** DO NOT do this:
    public float ExtGlow;
    public int GetTheLatestWebSiteAccessTimeInMilliseconds()
    
    // **βœ…** DO this instead:
    public float ExternalGlow;
    public int GetLatestWebAccessTimeMillis()
    
    • Any Abbreviations must be widely known and accepted.

      public int Atk;
      public int Def;
      public float Pow;
      
  • ❌ DO NOT use C# reserved words as names.

  • ❌ AVOID naming conflicts with existing .NET Framework namespaces, or types.

  • ❌ AVOID adding redundant or meaningless prefixes and suffixes to identifiers

    // **❌** DO NOT do this:
    public enum ColorsEnum
    public class CVehicle
    public struct RectangleStruct
    
    // **βœ…** DO this instead:
    public enum Colors
    public class Vehicle
    public struct Rectangle
    
  • ❌ DO NOT include the parent class name within a property name.

    // **❌** DO NOT do this:
    public class Customer {
        public int CustomerName;
    }
    
    // **βœ…** DO this instead:
    public class Customer {
        public int Name;
    }
    
  • βœ… TRY to prefix boolean variables and properties with β€œCan”, β€œIs” or β€œHas”.

  • Append computational qualifiers to variable names like Average, Count, Sum, Min, and Max where appropriate.

  • When defining a root namespace, use a Product, Company, or Developer Name as the root.

    Agate.ProductName
    

Naming Usage


Namespace

See Namespace Standard

Source Files

Capitalization: PascalCase

βœ… ALWAYS match class name and file name.

❌ AVOID including more than one class, enum (global), or delegate (global) per file.

Use a descriptive file name when containing multiple class, enum (global), or delegate (global).

MyClass.cs
public class MyClass {…}

Class or Struct

Capitalization: PascalCase

Use a noun or noun phrase for class name. Add an appropriate class-suffix when sub-classing another type when possible.

private class MyClass {…} 
internal class SpecializedAttribute : Attribute {…} 
public class CustomerCollection : CollectionBase {…} 
public class CustomEventArgs : EventArgs {…} 
private struct GameSettings {…}

Interface

Capitalization: PascalCase

βœ… ALWAYS prefix interface name with capital β€œI”

interface ICreature {…}
interface IWeapon

Generic Class

Capitalization: PascalCase

βœ… ALWAYS use a single capital letter, such as T or K.

public class FifoStack<T> {
		public void Push(T obj) {…}
		public T Pop() {…}
}

Method

Capitalization: PascalCase

βœ… TRY to use a Verb or Verb-Object pair.

public void Execute() {…}
private string GetAssemblyVersion(Assembly target) {…}

Property

Capitalization: PascalCase

Property name should represent the entity it returns. Never prefix property names with β€œGet” or β€œSet”.

// DO NOT do this:
public string GetName { 
		get{…}
}

// DO this instead:
public string Name { 
		get{…} 
		set{…} 
}

Field (Public, Protected, or Internal)

Capitalization: PascalCase

❌ AVOID using non-private Fields! Use Properties instead.

public string Name;
protected IList InnerList;

Field (Private)

Capitalization: camelCase

Prefix with a single underscore ( _ ) character.

private string _name;

Constant or Static Field

Treat like a Field. Choose appropriate Field access-modifier above.

Enum

Capitalization: PascalCase (both the Type and the Options).

Add the FlagsAttribute to bit-mask multiple options.

public enum CustomerTypes
{
		Consumer,
		Commercial
}

Delegate or Event

Treat as a Field. Choose appropriate Field access-modifier above.

public event EventHandler LoadPlugin;

Variable (inline)

Capitalization: camelCase

  • ❌ AVOID using single characters like β€œx” or β€œy” except in FOR loops.
  • ❌ AVOID enumerating variable names like text1, text2, text3 etc.

Parameter

Capitalization: camelCase

public void Execute(string commandText, int iterations) {…}

See Also


Coding Standard

Namespace Standard

Foldering Standard

Git Usage Standard

Back to Top ⬆️