Home Backend Development C#.Net Tutorial .NET Framework-C# programming style code examples given by Microsoft

.NET Framework-C# programming style code examples given by Microsoft

Mar 18, 2017 am 11:39 AM



from ms official guideline:

1 We use Allman style braces, where each brace begins on a new line.

while (x == y){
    something();
    somethingelse();
}finalthing();
Copy after login

2 We use four spaces of indentation (no tabs).

3 We use camelCase for internal and private fields and use readonly where possible. Prefix instance fields with , static fields with s_ and thread static fields with t_. When used on static fields, readonly should come after static (i.e. static readonly not readonly static).

4 We avoid this. unless absolutely necessary.

5 We always specify the visibility, even if it’s the default. Visibility should be the first modifier.

private string _foo //betterstring _foo  //bad
Copy after login
public abstract //betterabstract public //bad
Copy after login

6 Namespace imports should be specified at the top of the file, outside of namespace declarations and should be sorted alphabetically.

using System.IO;using System.Collections;

namespace CAXA.MES.UI.Performance.Board
{  public class LinkedList
  {


  }

}
Copy after login

7 Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.

8 Avoid spurious free spaces.

if (someVar == 0)...,
Copy after login

9 If a file happens to differ in style from these guidelines (e.g. private members are named m_member rather than _member), the existing style in that file takes precedence.

10 We only use var when it’s obvious what the variable type is.

var stream = new FileStream(...) //var is OKay
var stream = OpenStandardInput() // here var is not good
Copy after login

11 We use language keywords instead of BCL types.

 int, string, float // good
 Int32, String, Single // bad
Copy after login

12 We use PascalCasing to name all our constant local variables and fields. The only exception is for interop code where the constant value should exactly match the name and value of the code you are calling via interop.

private const int Age=100; //good
Copy after login

13 We use nameof(…) instead of “…” whenever possible and relevant.

14 Fields should be specified at the top within type declarations.

15 When including non-ASCII characters in the source code use Unicode escape sequences (\uXXXX) instead of literal characters.

The above is the detailed content of .NET Framework-C# programming style code examples given by Microsoft. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Active Directory with C#

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Access Modifiers in C#

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Random Number Generator in C#

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

C# Data Grid View

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Patterns in C#

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

C# StringReader

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

C# StringWriter

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

BinaryWriter in C#

See all articles