C# Coding Best Practices
C# Coding Best Practices
C# Coding Best Practices
• Article
• 09/29/2022
• 11 minutes to read
• 6 contributors
Feedback
• They
create a consistent look to the code, so that readers can focus on content,
not layout.
• They enable readers to understand the code more quickly by making
assumptions based on previous experience.
• They facilitate copying, changing, and maintaining the code.
• They demonstrate C# best practices.
Important
The guidelines in this article are used by Microsoft to develop samples and
documentation. They were adopted from the .NET Runtime, C# Coding
Style guidelines. You can use them, or adapt them to your needs. The primary
objectives are consistency and readability within your project, team, organization, or
company source code.
Naming conventions
There are several naming conventions to consider when writing C# code.
In the following examples, any of the guidance pertaining to elements marked public is
also applicable when working with protected and protected internal elements, all of
which are intended to be visible to external callers.
Pascal case
C#Copy
public class DataService
{
}
C#Copy
public record PhysicalAddress(
string Street,
string City,
string StateOrProvince,
string ZipCode);
C#Copy
public struct ValueCoordinate
{
}
When naming an interface, use pascal casing in addition to prefixing the name with
an I. This clearly indicates to consumers that it's an interface.
C#Copy
public interface IWorkerQueue
{
}
When naming public members of types, such as fields, properties, events, methods, and
local functions, use pascal casing.
C#Copy
public class ExampleEvents
{
// A public field, these should be used sparingly
public bool IsValid;
// An init-only property
public IWorkerQueue WorkerQueue { get; init; }
// An event
public event Action EventProcessing;
// Method
public void StartEventProcessing()
{
// Local function
static int CountQueueItems() => WorkerQueue.Count;
// ...
}
}
When writing positional records, use pascal casing for parameters as they're the public
properties of the record.
C#Copy
public record PhysicalAddress(
string Street,
string City,
string StateOrProvince,
string ZipCode);
For more information on positional records, see Positional syntax for property definition.
Camel case
Use camel casing ("camelCasing") when naming private or internal fields, and prefix
them with _.
C#Copy
public class DataService
{
private IWorkerQueue _workerQueue;
}
Tip
When editing C# code that follows these naming conventions in an IDE that supports
statement completion, typing _ will show all of the object-scoped members.
When working with static fields that are private or internal, use the s_ prefix and for
thread static use t_.
C#Copy
public class DataService
{
private static IWorkerQueue s_workerQueue;
[ThreadStatic]
private static TimeSpan t_timeSpan;
}
C#Copy
public T SomeMethod<T>(int someNumber, bool isValid)
{
}
• You don't have to change the names of objects that were created by using
the Visual Studio designer tools to make them fit other guidelines.
Layout conventions
Good layout uses formatting to emphasize the structure of your code and to make the
code easier to read. Microsoft examples and samples conform to the following
conventions:
C#Copy
if ((val1 > val2) && (val1 > val3))
{
// Take appropriate action.
}
Commenting conventions
• Place the comment on a separate line, not at the end of a line of code.
• Begin comment text with an uppercase letter.
• End comment text with a period.
• Insert one space between the comment delimiter (//) and the comment text,
as shown in the following example.
C#Copy
// The following declaration creates a query. It does not run
// the query.
• Don't create formatted blocks of asterisks around comments.
• Ensure all public members have the necessary XML comments providing
appropriate descriptions about their behavior.
Language guidelines
The following sections describe practices that the C# team follows to prepare code
examples and samples.
C#Copy
string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
C#Copy
var phrase =
"lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
var manyPhrases = new StringBuilder();
for (var i = 0; i < 10000; i++)
{
manyPhrases.Append(phrase);
}
//Console.WriteLine("tra" + manyPhrases);
• Use implicit typing for local variables when the type of the variable is
obvious from the right side of the assignment, or when the precise type is
not important.
C#Copy
var var1 = "This is clearly a string.";
var var2 = 27;
• Don't use var when the type is not apparent from the right side of the
assignment. Don't assume the type is clear from a method name. A variable
type is considered clear if it's a new operator or an explicit cast.
C#Copy
int var3 = Convert.ToInt32(Console.ReadLine());
int var4 = ExampleClass.ResultSoFar();
• Don't rely on the variable name to specify the type of the variable. It might
not be correct. In the following example, the variable name inputInt is
misleading. It's a string.
C#Copy
var inputInt = Console.ReadLine();
Console.WriteLine(inputInt);
• Avoid the use of var in place of dynamic. Use dynamic when you want run-
time type inference. For more information, see Using type dynamic (C#
Programming Guide).
• Use implicit typing to determine the type of the loop variable in for loops.
C#Copy
var phrase =
"lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
var manyPhrases = new StringBuilder();
for (var i = 0; i < 10000; i++)
{
manyPhrases.Append(phrase);
}
//Console.WriteLine("tra" + manyPhrases);
• Don't use implicit typing to determine the type of the loop variable
in foreach loops. In most cases, the type of elements in the collection isn't
immediately obvious. The collection's name shouldn't be solely relied upon
for inferring the type of its elements.
C#Copy
foreach (char ch in laugh)
{
if (ch == 'h')
Console.Write("H");
else
Console.Write(ch);
}
Console.WriteLine();
Note
Be careful not to accidentally change a type of an element of the iterable
collection. For example, it is easy to switch
from System.Linq.IQueryable to System.Collections.IEnumerable in
a foreach statement, which changes the execution of a query.
In general, use int rather than unsigned types. The use of int is common throughout
C#, and it is easier to interact with other libraries when you use int.
Arrays
Use the concise syntax when you initialize arrays on the declaration line. In the following
example, note that you can't use var instead of string[].
C#Copy
string[] vowels1 = { "a", "e", "i", "o", "u" };
C#Copy
var vowels2 = new string[] { "a", "e", "i", "o", "u" };
Delegates
Use Func<> and Action<> instead of defining delegate types. In a class, define the
delegate method.
C#Copy
public static Action<string> ActionExample1 = x => Console.WriteLine($"x is: {x}");
Call the method using the signature defined by the Func<> or Action<> delegate.
C#Copy
ActionExample1("string for x");
If you create instances of a delegate type, use the concise syntax. In a class, define the
delegate type and a method that has a matching signature.
C#Copy
public delegate void Del(string message);
Create an instance of the delegate type and call it. The following declaration shows the
condensed syntax.
C#Copy
Del exampleDel2 = DelMethod;
exampleDel2("Hey");
C#Copy
Del exampleDel1 = new Del(DelMethod);
exampleDel1("Hey");
C#Copy
static string GetValueFromArray(string[] array, int index)
{
try
{
return array[index];
}
catch (System.IndexOutOfRangeException ex)
{
Console.WriteLine("Index is out of range: {0}", index);
throw;
}
}
• Simplify your code by using the C# using statement. If you have a try-
finally statement in which the only code in the finally block is a call to
the Dispose method, use a using statement instead.
C#Copy
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
{
((IDisposable)font1).Dispose();
}
}
C#Copy
using (Font font2 = new Font("Arial", 10.0f))
{
byte charset2 = font2.GdiCharSet;
}
C#Copy
using Font font3 = new Font("Arial", 10.0f);
byte charset3 = font3.GdiCharSet;
C#Copy
Console.Write("Enter a dividend: ");
int dividend = Convert.ToInt32(Console.ReadLine());
If the divisor is 0, the second clause in the if statement would cause a run-time error.
But the && operator short-circuits when the first expression is false. That is, it doesn't
evaluate the second expression. The & operator would evaluate both, resulting in a run-
time error when divisor is 0.
new operator
• Use one of the concise forms of object instantiation, as shown in the
following declarations. The second example shows syntax that is available
starting in C# 9.
C#Copy
var instance1 = new ExampleClass();
C#Copy
ExampleClass instance2 = new();
C#Copy
ExampleClass instance2 = new ExampleClass();
C#Copy
var instance3 = new ExampleClass { Name = "Desktop", ID = 37414,
Location = "Redmond", Age = 2.3 };
The following example sets the same properties as the preceding example
but doesn't use initializers.
C#Copy
var instance4 = new ExampleClass();
instance4.Name = "Desktop";
instance4.ID = 37414;
instance4.Location = "Redmond";
instance4.Age = 2.3;
Event handling
If you're defining an event handler that you don't need to remove later, use a lambda
expression.
C#Copy
public Form2()
{
this.Click += (s, e) =>
{
MessageBox.Show(
((MouseEventArgs)e).Location.ToString());
};
}
C#Copy
public Form1()
{
this.Click += new EventHandler(Form1_Click);
}
Static members
Call static members by using the class name: ClassName.StaticMember. This practice
makes code more readable by making static access clear. Don't qualify a static member
defined in a base class with the name of a derived class. While that code compiles, the
code readability is misleading, and the code may break in the future if you add a static
member with the same name to the derived class.
LINQ queries
C#Copy
var seattleCustomers = from customer in customers
where customer.City == "Seattle"
select customer.Name;
• Use aliases to make sure that property names of anonymous types are
correctly capitalized, using Pascal casing.
C#Copy
var localDistributors =
from customer in customers
join distributor in distributors on customer.City equals
distributor.City
select new { Customer = customer, Distributor = distributor };
C#Copy
var localDistributors2 =
from customer in customers
join distributor in distributors on customer.City equals
distributor.City
select new { CustomerName = customer.Name, DistributorID =
distributor.ID };
• Use implicit typing in the declaration of query variables and range variables.
C#Copy
var seattleCustomers = from customer in customers
where customer.City == "Seattle"
select customer.Name;
• Align query clauses under the from clause, as shown in the previous
examples.
• Use where clauses before other query clauses to ensure that later query
clauses operate on the reduced, filtered set of data.
C#Copy
var seattleCustomers2 = from customer in customers
where customer.City == "Seattle"
orderby customer.Name
select customer;
• Use multiple from clauses instead of a join clause to access inner collections.
For example, a collection of Student objects might each contain a collection
of test scores. When the following query is executed, it returns each score
that is over 90, along with the last name of the student who received the
score.
C#Copy
var scoreQuery = from student in students
from score in student.Scores!
where score > 90
select new { Last = student.LastName, score };