C# 程式規範


分類

建立時間: 2022年6月26日 21:56
更新時間: 2022年11月19日 17:01

說明

原本這篇要寫程式風格的筆記,但我改以 StyleCop Analyzers 做為我寫程式的規範,在 Visual Studio 可以安裝 StyleCop.Analyzers 套件,這真的是一個很嚴格的檢查程式風格的套件,所以我就比較不會太常更新這篇內容,筆記主要會寫一些比較常用或很值得分享給大家的內容,請斟酌參考就好,不一定要完全遵守。

StyleCop Analyzers

StyleCop 是一套 C# 程式規範

Visual Studio

如果你使用 Visual Studio 或 Visual Studio for Mac 寫 c#,你可以在 NuGet 套件中找到 StyleCop.Analyzers

StyleCop Analyzers install in NuGet

詳細的安裝內容請參考 StyleCopAnalyzers GitHub Installation

符合 StyleCop 的程式預覽

Card.cs

// <copyright file="Card.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace CardLinq
{
    using System;

    /// <summary>
    /// 撲克牌.
    /// </summary>
    public class Card : IComparable<Card>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Card"/> class.
        /// </summary>
        /// <param name="value">Card value.</param>
        /// <param name="suit">Card suit.</param>
        public Card(Values value, Suits suit)
        {
            this.Suit = suit;
            this.Value = value;
        }

        /// <summary>
        /// Gets card suit.
        /// </summary>
        public Suits Suit
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets card value.
        /// </summary>
        public Values Value
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets card name with suit and value.
        /// </summary>
        public string Name
        {
            get
            {
                return $"{this.Value} of {this.Suit}";
            }
        }

        /// <summary>
        /// Compare by value.
        /// </summary>
        /// <param name="other">another card.</param>
        /// <returns>comparation result.</returns>
        public int CompareTo(Card? other)
        {
            return new CardComparerByValue().Compare(this, other);
        }

        /// <summary>
        /// Show card name.
        /// </summary>
        /// <returns>card name.</returns>
        public override string ToString()
        {
            return this.Name;
        }
    }
}

以下為筆記

命名方式

Pascal 大小寫

每個單字的第一個字為大寫「PascalCasing」

Camel 大小寫

除了第一個單字的第一個字母為小寫以外,每個單字的第一個字為大寫「camelCasing」

類別

類別名稱

在命名 class 時,使用 Pascal

public class DataService
{
}

public 和 protected

命名 public 和 protected 型別的成員時,例如欄位、屬性、事件、方法和區域函式,請使用 pascal 大小寫。

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;
        // ...
    }
}

private

在命名 private 或 internal 欄位時使用 camel 大小寫 (「camelCasing」) ,並以 _ 作為前置詞。

public class DataService
{
    private IWorkerQueue _workerQueue;
}

在 StyleCop 中,無需加 _

public class Review
{
    private double score;
}

static

使用 private 或 internal 的 static 字段時,static 請使用 s_ 前置詞,執行緒使用 t_。

public class DataService
{
    private static IWorkerQueue s_workerQueue;

    [ThreadStatic]
    private static TimeSpan t_timeSpan;
}

方法參數

撰寫方法參數時,請使用 camel 大小寫。

public T SomeMethod<T>(int someNumber, bool isValid)
{
}

區域變數

雖然我沒看到官方文件提到關於區域變數命名習慣,但我看到的大部分都是 camel 大小寫。

var camelCaseVariable = 1;

常數欄位使用全大寫 + _

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example
{
    internal class ExampleClass
    {
        // const 使用全大寫 + _
        public const int MAGAZINE_SIZE = 16;
    }
}

介面

介面名稱以 I 開頭

interface ICar
{
}

參考

觀看次數: 776
c#StyleCop程式規範
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!