관리 메뉴

지식 창고

C# Coding Standards 본문

Huvitz/Project

C# Coding Standards

Lucky-John 2022. 4. 6. 11:25

1. To declare a variable which returns a single entity/object.

var item = new Item();

 

2. To declare a variable which returns multiple entity/objects means add "s" or "List" suffix, so we can easily

   identify that it will return a list of classes/objects.

var itemList = new List<Item>();

 

3. To declare a private variable, we use "_".

private int _nValue = 10;

 

4. To declare a variable, we use by uppercase letters.

public int nValue = 10;

 

5. When declaring a data type, write is as follows.

1) Integer : int nValue;
2) Float : float fValue;
3) Bool or boolean : boolean isValue;
4) Double : double dValue;
5) String or string : string strValue;
6) Array : arrValue;
7) List : List itemList

 

6. It is recommended to use "string.Empty" for string initialization.

string strValue = string.Empty;

 

 

7. To declare an empty method which only returns a view in MVC, we should use the expression body. 

//Avoid
public ActionResult Dashboard()
{
    return View();
}

//Do
public ActionResult Dashboard() => View();

 

8. To check null or empty condition. 

//Avoid var varName = "faisal";
if (varName != null && varName != "")
{
   //code
}

//Do
var varName = "faisal";
if (!string.IsNullOrEmpty(varName))
{
   //code
}

 

9. Use null coalescing expression. that is, the ternary operator is not recommended

Test test = new Test();

//Avoid
var varName = test.Name != null ? test.Name : "";

//Do
var varName = test.Name ?? "";

 

10. Use object initializer

//Avoid
Test test = new Test();
test.Id = 1;
test.Name = "faisal";

//Do
var test = new Test
{
   Id = 1,
   Name = "faisal"
};

 

11. Use ?. operator

//Avoid
var empName = "";
Session["Name"] = "Faisal Pathan";
if (Session["Name"] != null)
{
   empName = Session["Name"].ToString();
}
else
{
    empName = "";
}

//Do
var empName = "";
Session["Name"] = "Faisal Pathan";
empName = Session["Name"]?.ToString() ?? "";

 

12. Use string interpolation

Test test = new Test();

//Avoid
var details = string.Format("{0}, you are welcome, Your Id is {1}", test.Name , test.Id + "_emp");

//Do
var details = $"{test.Name}, you are welcome, Your Id is {test.Id}_emp";

 

13. New lightweight switchcase with c# 8

int itemSwitch = 1;

//Good
switch (itemSwitch)
{
   case 1:
        Console.WriteLine("Item 1");
        break;
    case 2:
         Console.WriteLine("Item 2");
         break;
    default:
          Console.WriteLine("Default item case");
          break;
}

//better
var message = itemSwitch switch
            {
                    1 => Console.WriteLine("Item 1"),
                    2 => Console.WriteLine("Item 2"),
                     _ => "Default item case"
             };
Console.WriteLine(message);

 

14. for statement is recommened as below.

List<ActionItem> itemList = new List<ActionItem>(); 
foreach(ActionItem item in itemList)
{
   // code
}

for(int i=0; i<10; i++)
{
    // code
}

 

15. It is recommended to use the constant declaration as shown below.

// good
public const string kStrExportDir = "Export";

// better
public readonly string kStrExportDir = "Export";

 

16. Enum 

public enum EnumDeclar
{
    Invalid = 0,
    Yes,
    No,
    YesNo,
    Cancel
}

 

 

Reference:

https://google.github.io/styleguide/csharp-style.html

 

C# at Google Style Guide

Style guides for Google-originated open-source projects

google.github.io

 

Comments