
While there might not be one right way to format your C# code, agreeing on a consistent style across your team can result in a cleaner, more readable and scalable codebase. A well-organized style guide can help you rein in discrepancies to produce a cohesive final product.
The names of your variables, classes, and methods aren’t mere labels. They carry weight and meaning. Good naming style impacts how someone reading your program can comprehend the idea you’re trying to convey.
This page provides tips and key considerations to keep in mind for naming conventions and code formatting when creating your own style guide.
Note: The recommendations shared here are based on those provided by Microsoft. The best code style guide rules are the ones that work best for your team’s needs.
You can find a code style guide example here or download the full e-book, Use a C# style guide for clean and scalable game code (Unity 6 edition).
An identifier is any name you assign to a type (class, interface, struct, delegate, or enum), member, variable, or namespace.
Avoid special characters (backslashes, symbols, Unicode characters) in your identifiers, even though C# permits them. These can interfere with certain Unity command-line tools. Steer clear of unusual characters to ensure compatibility with most platforms.
You can’t define variables with spaces in the name because C# uses the space character to separate identifiers. Casing schemes can alleviate the issue of having to use compound names or phrases in source code.
Listed below are several well-known naming and casing conventions:
Camel case (camelCase)
Also known as camel caps, camel case is the practice of writing phrases without spaces or punctuation, separating words with a single capitalized letter. The very first letter is lowercase. Local variables and method parameters are camel case. For example:
examplePlayerController
maxHealthPoints
endOfFile
Pascal case (PascalCase)
Pascal case is a variation of camel case, where the initial letter is capitalized. Use this for class, public fields and method names in Unity development. For example:
ExamplePlayerController
MaxHealthPoints
EndOfFile
Snake case (snake_case)
In this case, spaces between words are replaced with an underscore character. For example:
example_player_controller
max_health_points
end_of_file
Kebab case (kebab-case)
Here, spaces between words are replaced with dashes. The words appear on a “skewer” of dash characters. For example:
example-player-controller
Max-health-points
end-of-file
naming-conventions-methodology
The Kebab-case is widely used in web technologies and namely for CSS. We are also recommending it for use with UI Toolkit USS.
Hungarian notation
The variable or function name often indicates its intention or type. For example:
int iCounter
string strPlayerName
Hungarian notation is an older convention and is not common in Unity development.
Consider these rules for your variables and fields:

Enums are special value types defined by a set of named constants. By default, the constants are integers, counting up from zero.
Use Pascal case for enum names and values. You can place public enums outside of a class to make them global. Use a singular noun for the enum name as it represents a single value from a set of possible values. They should have no prefix or suffix.
Note: Bitwise enums marked with the System.FlagsAttribute attribute are the exception to this rule. You typically pluralize these as they represent more than one type.
Follow these standard rules when naming your classes and interfaces:
In C#, every executed instruction is performed in the context of a method.
Note: “function” and “method” are often used interchangeably in Unity development. However, because you can’t write a function without incorporating it into a class in C#, “method” is the accepted term.
Methods perform actions, so apply these rules to name them accordingly:
Events in C# implement the observer pattern. This software design pattern defines a relationship in which one object, the subject (or publisher), can notify a list of dependent objects called observers (or subscribers). Thus, the subject can broadcast state changes to its observers without tightly coupling the objects involved. You can learn more about using the observer and other design patterns in your Unity projects in the e-book Level up your code with design patterns and SOLID.
Name the event with a verb phrase. Choose a name that communicates the state change accurately. Use the present or past participle to indicate events “before” or “after.” For example, specify “OpeningDoor” for an event before opening a door or “DoorOpened” for an event afterward.
In most cases, the Action<T> delegate can handle the events needed for gameplay. You may pass anywhere from 0 to 16 input parameters of different types with a return type of void. Using the predefined delegate saves code.
Note: You can also use the EventHandler or EventHandler<TEventArgs> delegates. Agree as a team on how everyone will implement events.
The subject that invokes the event typically does so from a method prefixed with “On,” e.g. “OnOpeningDoor” or “OnDoorOpened.”
If the subject is named “GameEvents,” your observers can have a method called “GameEvents_OpeningDoor” or “GameEvents_DoorOpened.” Note that this is called the “event handling method”, not to be confused with the EventHandler delegate.
Create custom EventArgs only as necessary. If you need to pass custom data to your Event, create a new type of EventArgs, either inherited from System.EventArgs or from a custom struct.
Use namespaces to ensure that your classes, interfaces, enums, etc. won’t conflict with existing ones from other namespaces or the global namespace. Namespaces can also prevent conflicts with third-party assets from the Asset Store.
When applying namespaces:
In code, these classes are referred to as Enemy.Controller1 and Enemy.Controller2, respectively. Add a using line to save typing out the prefix:
using Enemy;
When the compiler finds the class names Controller1 and Controller2, it understands you mean Enemy.Controller1 and Enemy.Controller2.
If the script needs to refer to classes with the same name from different namespaces, use the prefix to differentiate them. For instance, if you have a Controller1 and Controller2 class in the Player namespace, you can write out Player.Controller1 and Player.Controller2 to avoid any conflicts. Otherwise, the compiler will report an error.
using Enemy;
Learn more about general formatting here or check out the full e-book. You can also explore our code style guide example.