2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】Enumにメソッドを追加する方法(拡張メソッド)

Posted at

前提

今回の記事では都市(City)の列挙体(Enum)から人工(Artificial)を取得したい場合を例とします。

拡張メソッドを知らなかった時の実装方法

拡張メソッドを知らなかった時は下記のように実装していました。

メソッドを使用した例
using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main()
        {
            var city = City.東京24;

            //ここでメソッドに列挙を渡して人工を取得する。
            var artificial = CityEnum.GetArtificial(city);

            Console.WriteLine(artificial);
        }
    }

    public enum City
    {
        東京24,
        横浜市,
        大阪市,
        名古屋市
    }

    public static class CityEnum
    {
        public static int GetArtificial(City city)
        {
            return city switch
            {
                City.東京24 => 9_671_000,
                City.横浜市 => 3_725_000,
                City.大阪市 => 2_691_000,
                City.名古屋市 => 2_296_000,
                _ => 0,
            };
        }
    }
}

下記で引数に指定された都市列挙体の人工を返すメソッドを実装しています。

GetArtificialメソッド
public static int GetArtificial(City city)
{
    return city switch
    {
        City.東京24 => 9_671_000,
        City.横浜市 => 3_725_000,
        City.大阪市 => 2_691_000,
        City.名古屋市 => 2_296_000,
        _ => 0,
    };
}

そして、実装した人工取得メソッドに東京24区列挙値を渡して人工を取得しています。

東京24区の人工を取得
var city = City.東京24;

var artificial = CityEnum.GetArtificial(city);

拡張メソッドを知っている場合の実装方法

拡張メソッドを知っていると下記のように実装できます。

拡張メソッドを使用した例
using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main()
        {
            var city = City.東京24;

            //ここでメソッドに列挙を渡して人工を取得する。
            var artificial = city.GetArtificial();

            Console.WriteLine(artificial);
        }
    }

    public enum City
    {
        東京24,
        横浜市,
        大阪市,
        名古屋市
    }

    public static class CityEnum
    {
        public static int GetArtificial(this City city)
        {
            return city switch
            {
                City.東京24 => 9_671_000,
                City.横浜市 => 3_725_000,
                City.大阪市 => 2_691_000,
                City.名古屋市 => 2_296_000,
                _ => 0,
            };
        }
    }
}

下記で都市列挙体(City)に対して人工を返す拡張メソッドを追加しています。

GetArtificialメソッド
public static int GetArtificial(this City city)
{
    return city switch
    {
        City.東京24 => 9_671_000,
        City.横浜市 => 3_725_000,
        City.大阪市 => 2_691_000,
        City.名古屋市 => 2_296_000,
        _ => 0,
    };
}

そして、追加した人工取得拡張メソッドを使用して人工を取得しています。

東京24区の人工を取得
var city = City.東京24;

var artificial = city.GetArtificial();

拡張メソッドの追加方法

拡張メソッドを追加するには引数のCity列挙型の前にthisをつけるだけです。

GetArtificialメソッド
//                              ↓ここにthisをつけるとCity列挙型の拡張メソッドになる
public static int GetArtificial(this City city)
{
    return city switch
    {
        City.東京24 => 9_671_000,
        City.横浜市 => 3_725_000,
        City.大阪市 => 2_691_000,
        City.名古屋市 => 2_296_000,
        _ => 0,
    };
}
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?