C#コーディングルール_#29_アクセスレベルを加味した書く順番

当サイトではアフィリエイト広告を利用しています。

C#コーディングルール

前回はプロパティとかコンストラクタとかどちらを上に書くべきか?といった内容をご説明しました。今回もコード内順序のお話ですが,アクセスレベルを加味したコード内順序のお話になります。アクセスレベルとはpublicなどのアクセス修飾子の事です。

アクセスレベルごとのコード内順序

アクセスレベルごとに,コード内の順序があり,それに違反すると警告が表示されます。基本的には,アクセスレベルの広い順番で書くという事になります。アクセスレベルはpublicが一番広く,privateが一番狭いですから,例えばpublicメソッドよりもprivateメソッドを上に記述すると警告が出ます。

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        private int GetData2(int productId)
        {
            return 0;
        }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public int GetData(int productId)
        {
            if (productId == 0)
            {
                productId = 1;
                return productId;
            }

            ////値を抽出してから返却します
            for (int i = 0; i < 10; i++)
            {
                productId = 0;
            }

            return 0;
        }

SA1202

アクセスレベルの順序

アクセスレベルの順序は次のように定められています。

  • public
  • internal
  • protected internal
  • protected
  • private

アクセスレベルは前述のコンストラクタやプロパティの順序内での順序となるので,プロパティはプロパティの中でアクセスレベル順になるようにします。ですのでpublicのプロパティ,internalのプロパティ,publicのメソッドというような順序で書きます。

       ///
        /// 顧客の名前を取得または設定します。
        /// 
        public string ProductName { get; set; }

        ///
        /// 顧客の名前を取得または設定します。
        /// 
        internal string ProductName2 { get; set; }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public int GetData(int productId)
        {
            if (productId == 0)
            {
                productId = 1;
                return productId;
            }

            ////値を抽出してから返却します
            for (int i = 0; i < 10; i++)
            {
                productId = 0;
            }

            return 0;
        }

この例のように,internalでもプロパティであれば,publicのメソッドより上に書く必要があります。それぞれの項目(プロパティなど)ごとに並べて,その中で,アクセスレベルの広い順と覚えてください。

同じアクセスレベルならStaticを上に書く

同じアクセスレベルでもstaticが優先されます。Internalとinternal staticであれば,internal staticを上に書く必要があります。

       ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public int GetData(int productId)
        {
            if (productId == 0)
            {
                productId = 1;
                return productId;
            }

            ////値を抽出してから返却します
            for (int i = 0; i < 10; i++)
            {
                productId = 0;
            }

            return 0;
        }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public static int GetDataStatic(int productId)
        {
            return 0;
        }

このように同じアクセスレベルでstaticを下に書くと警告が表示されます。

SA1204

同じアクセスレベルではstaticが優先となります。次のように修正することで警告が消えます。

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public static int GetDataStatic(int productId)
        {
            return 0;
        }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public int GetData(int productId)
        {
            if (productId == 0)
            {
                productId = 1;
                return productId;
            }

            ////値を抽出してから返却します
            for (int i = 0; i < 10; i++)
            {
                productId = 0;
            }

            return 0;
        }

ProductEntity全体

最後にProductEntity全体のコードをご参考に記載しておきます。

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

namespace Anderson.MX99.WinForm.Objects
{
    ///
    /// 商品エンティティ
    /// 
    internal sealed class ProductEntity
    {
        ////フィールド
        ////コンストラクタ
        ////デリゲート
        ////イベント
        ////列挙体
        ////プロパティ
        ////メソッド

        ///
        /// 値
        /// 
        private int _productId = 0;

        ///
        /// コンストラクタ
        /// 
        public ProductEntity()
        {
        }

        ///
        /// デストラクタ
        /// 
        ~ProductEntity()
        {
        }

        ///
        /// 商品が変更されたときに通知されます
        /// 
        public event Action ProductChanged;

        ///
        /// 顧客の名前を取得または設定します。
        /// 
        public string ProductName { get; set; }

        ///
        /// 顧客の名前を取得または設定します。
        /// 
        internal string ProductName2 { get; set; }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public static int GetDataStatic(int productId)
        {
            return 0;
        }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        public int GetData(int productId)
        {
            if (productId == 0)
            {
                productId = 1;
                return productId;
            }

            ////値を抽出してから返却します
            for (int i = 0; i < 10; i++)
            {
                productId = 0;
            }

            return 0;
        }

        ///
        /// 値の取得
        /// 
        ///商品ID
        /// 値
        private int GetData2(int productId)
        {
            return 0;
        }

        ///
        /// 設定
        /// 
        private void SetValue()
        {
        }
    }
}