C# Coding Standards

C# Coding Standards

The reason why coding standards are important is that they help to ensure safety, security, and reliability. Every development team should use one. Even the most experienced developer could introduce a coding defect — without realizing it. And that one defect could lead to a minor glitch. 

C# Microsoft Coding Standards 

Class, Function and Property names 

Use pascal casing (“PascalCasing“) for class, record, struct, functions, property names and namespaces 

namespace WebApplication6.Models 
{ 
    public class TestClass 
    { 

        public string UserName { get; set; }  

        public void ConcatenateUserName(string toConcatenateString) 
        { 
            ...... 
        } 

        public interface IWorkerQueue 
        { 

        } 
    } 
} 

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. 

When naming public members of types, such as fields, properties, events, methods, and local functions, use pascal casing.

Variables and Arguments names 

Use camel casing (“camelCasing“) for local variables and function arguments 

when naming private or internal fields use camel casing prefix them with _ 

when naming Thread or static fields use camel casing for static prefix s_ and for thread use t_. 

Use a description of the function argument when naming it 

//Correct  

public static void ConcatenateUserName(string toConcatenateString) 

            Thread.Sleep(1000); 
}

//Incorrect  

public static void ConcatenateUserName(string arg) 
{ 
            Thread.Sleep(1000); 
}

Abbreviations 

Avoid Abbreviations unless it is commonly used e.g Id, FTP, etc 

//Correct 

UserDetails userDetails; 

//Incorrect 

UserDetails un; 

//Exception 

UserId uId; 

Naming Convention

Use nouns or noun phrases to name a class 

public class UserDetails 
{ 
  …….. 
}

Name files according to their main classes except partial classes which should reflect their source or purpose 

Do not add multiple classes in a single “.cs” file with the exception of inner classes 

Enums 

DO NOT suffix enum names with “Enum“ 

//Correct  

public enum Level 
{ 
        Low, 

        Medium, 

        High 

}

//Incorrect 

public enum LevelEnum 
{ 

        Low, 

        Medium, 

        High 

}

Add summary for your methods and functions 

In Visual Studio type “///“ above your function or class and it will add a template automatically for you to fill. 

/// <summary> 

///  

/// </summary>

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 

// The following declaration creates a query. It does not run 

// the query. 

LINQ queries 

Use meaningful names for query variables. The following example uses seattleCustomers for customers who are located in Seattle. 

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. 

var localDistributors =  from customer in customers 

join distributor in distributors on customer.City equals distributor.City 

select new { Customer = customer, Distributor = distributor }; 

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. 

var seattleCustomers2 = from customer in customers 

                        where customer.City == "Seattle" 

                        orderby customer.Name 

                        select customer;

Unit Test Standards 

Naming standards are important because they explicitly express the intent of the test. Tests are more than just making sure your code works, they also provide documentation. Just by looking at the suite of unit tests, you should be able to infer the behavior of your code without even looking at the code itself. Additionally, when tests fail, you can see exactly which scenarios don’t meet your expectations. 

UnitOfWork_StateUnderTest_ExpectedBehavior 

//Correct 

[Fact] 

public void Add_SingleNumber_ReturnsSameNumber() 

{ 

   var stringCalculator = new StringCalculator(); 

   var actual = stringCalculator.Add("0"); 

   Assert.Equal(0, actual); 

}

//Incorrect 

[Fact] 

public void Test_Single() 

{ 

  var stringCalculator = new StringCalculator(); 

  var actual = stringCalculator.Add("0"); 

   Assert.Equal(0, actual); 

}

Arranging your tests 

Arrange, Act, Assert is a common pattern when unit testing. As the name implies, it consists of three main actions: 

  • Arrange your objects, create and set them up as necessary. 
  • Act on an object. 
  • Assert that something is as expected. 

//Correct 

[Fact] 

public void Add_EmptyString_ReturnsZero() 

{ 

    // Arrange 

    var stringCalculator = new StringCalculator(); 

    // Act 

    var actual = stringCalculator.Add(""); 

    // Assert 

    Assert.Equal(0, actual); 

 }

//Incorrect 

[Fact] 

public void Add_EmptyString_ReturnsZero() 

{ 

    // Arrange 

    var stringCalculator = new StringCalculator(); 

    // Assert 

    Assert.Equal(0, stringCalculator.Add("")); 

 }