C# style guide¶
Having well-defined and consistent coding conventions is important for every project, and Godot is no exception to this rule.
This page contains a coding style guide, which is followed by developers of and contributors to Godot itself. As such, it is mainly intended for those who want to contribute to the project, but since the conventions and guidelines mentioned in this article are those most widely adopted by the users of the language, we encourage you to do the same, especially if you do not have such a guide yet.
Note
This article is by no means an exhaustive guide on how to follow the standard coding conventions or best practices. If you feel unsure of an aspect which is not covered here, please refer to more comprehensive documentation, such as C# Coding Conventions or Framework Design Guidelines.
Language specification¶
Godot currently uses C# version 7.0 in its engine and example source code. So, before we move to a newer version, care must be taken to avoid mixing language features only available in C# 7.1 or later.
For detailed information on C# features in different versions, please see What's New in C#.
Formatting¶
General guidelines¶
Use line feed (LF) characters to break lines, not CRLF or CR.
Use one line feed character at the end of each file, except for csproj files.
Use UTF-8 encoding without a byte order mark.
Use 4 spaces instead of tabs for indentation (which is referred to as "soft tabs").
Consider breaking a line into several if it's longer than 100 characters.
Line breaks and blank lines¶
For a general indentation rule, follow the "Allman Style" which recommends placing the brace associated with a control statement on the next line, indented to the same level:
// Use this style:
if (x > 0)
{
DoSomething();
}
// NOT this:
if (x > 0) {
DoSomething();
}
However, you may choose to omit line breaks inside brackets:
For simple property accessors.
For simple object, array, or collection initializers.
For abstract auto property, indexer, or event declarations.
// You may put the brackets in a single line in following cases:
public interface MyInterface
{
int MyProperty { get; set; }
}
public class MyClass : ParentClass
{
public int Value
{
get { return 0; }
set
{
ArrayValue = new [] {value};
}
}
}
Insert a blank line:
After a list of
using
statements.Between method, properties, and inner type declarations.
At the end of each file.
Field and constant declarations can be grouped together according to relevance. In that case, consider inserting a blank line between the groups for easier reading.
Avoid inserting a blank line:
After
{
, the opening brace.Before
}
, the closing brace.After a comment block or a single-line comment.
Adjacent to another blank line.
using System;
using Godot;
// Blank line after `using` list.
public class MyClass
{ // No blank line after `{`.
public enum MyEnum
{
Value,
AnotherValue // No blank line before `}`.
}
// Blank line around inner types.
public const int SomeConstant = 1;
public const int AnotherConstant = 2;
private Vector3 _x; // Related constants or fields can be
private Vector3 _y; // grouped together.
private float _width;
private float _height;
public int MyProperty { get; set; }
// Blank line around properties.
public void MyMethod()
{
// Some comment.
AnotherMethod(); // No blank line after a comment.
}
// Blank line around methods.
public void AnotherMethod()
{
}
}
Using spaces¶
Insert a space:
Around a binary and ternary operator.
Between an opening parenthesis and
if
,for
,foreach
,catch
,while
,lock
orusing
keywords.Before and within a single line accessor block.
Between accessors in a single line accessor block.
After a comma which is not at the end of a line.
After a semicolon in a
for
statement.After a colon in a single line
case
statement.Around a colon in a type declaration.
Around a lambda arrow.
After a single-line comment symbol (
//
), and before it if used at the end of a line.
Do not use a space:
After type cast parentheses.
Within single line initializer braces.
The following example shows a proper use of spaces, according to some of the above mentioned conventions:
public class MyClass<A, B> : Parent<A, B>
{
public float MyProperty { get; set; }
public float AnotherProperty
{
get { return MyProperty; }
}
public void