Framework Guidelines

Edit this page

Use C# type aliases instead of the types from the System namespace (AV2201)

For instance, use object instead of Object, string instead of String, and int instead of Int32. These aliases have been introduced to make the primitive types first class citizens of the C# language, so use them accordingly.

Exception: When referring to static members of those types, it is custom to use the full CLS name, e.g. Int32.Parse() instead of int.Parse(). The same applies to members that need to specify the type they return, e.g. ReadInt32, GetUInt16.

Properly name properties, variables or fields referring to localized resources (AV2205)

The guidelines in this topic apply to localizable resources such as error messages and menu text.

  • Use Pascal casing in resource keys.
  • Provide descriptive identifiers rather than short ones. Keep them concise where possible, but don’t sacrifice readability.
  • Use only alphanumeric characters in naming resources.

Don’t hard-code strings that change based on the deployment (AV2207)

Examples include connection strings, server addresses, etc. Use Resources, the ConnectionStrings property of the ConfigurationManager class, or the Settings class generated by Visual Studio. Maintain the actual values into the app.config or web.config (and most definitely not in a custom configuration store).

Build with the highest warning level (AV2210)

Configure the development environment to use Warning Level 4 for the C# compiler, and enable the option Treat warnings as errors . This allows the compiler to enforce the highest possible code quality.

Properly fill the attributes of the AssemblyInfo.cs file (AV2215)

Ensure that the attributes for the company name, description, copyright statement, version, etc. are filled. One way to ensure that version and other fields that are common to all assemblies have the same values, is to move the corresponding attributes out of the AssemblyInfo.cs into a SolutionInfo.cs file that is shared by all projects within a solution.

Avoid LINQ for simple expressions (AV2220)

Rather than:

var query = from item in items where item.Length > 0 select item;

prefer the use of extension methods from the System.Linq namespace:

var query = items.Where(item => item.Length > 0);

Since LINQ queries should be written out over multiple lines for readability, the second example is a bit more compact.

Use Lambda expressions instead of anonymous functions (AV2221)

Lambda expressions provide a much more elegant alternative for anonymous functions. So instead of:

Customer customer = Array.Find(customers, delegate(Customer customer)
{
	return customer.Name == "Tom";
});

use a Lambda expression:

Customer customer = Array.Find(customers, customer => customer.Name == "Tom");

Or even better:

var customer = customers.Where(customer => customer.Name == "Tom").FirstOrDefault();

Only use the dynamic keyword when talking to a dynamic object (AV2230)

The dynamic keyword has been introduced for working with dynamic languages. Using it introduces a serious performance bottleneck because the compiler has to generate some complex Reflection code.

Use it only for calling methods or members of a dynamically created instance class (using the Activator) as an alternative to Type.GetProperty() and Type.GetMethod(), or for working with COM Interop types.

Favor async/await over the Task (AV2235)

Using the new C# 5.0 keywords results in code that can still be read sequentially and also improves maintainability a lot, even if you need to chain multiple asynchronous operations. For example, rather than defining your method like this:

public Task GetDataAsync()
{
  return MyWebService.FetchDataAsync()
    .ContinueWith(t => new Data(t.Result));
}

define it like this:

public async Task<Data> GetDataAsync()
{
  string result = await MyWebService.FetchDataAsync();
  return new Data(result);
}

Tip: Even if you need to target .NET Framework 4.0 you can use the async and await keywords. Simply install the Async Targeting Pack.