0

以下の列挙型の変数に対して次のように出力される C# コードはどれですか?

歯医者 (2533)

public enum eOccupationCode
        {
             Butcher = 2531,
             Baker = 2532,
             Dentist = 2533,
             Podiatrist = 2534,
             Surgeon = 2535,
             Other = 2539
        }
4

4 に答える 4

7

書式文字列 g, G,fを使用してF、列挙エントリの名前を出力したりdD10 進数表現を出力したりすることもできます。

var dentist = eOccupationCode.Dentist;

Console.WriteLine(dentist.ToString("G"));     // Prints: "Dentist"
Console.WriteLine(dentist.ToString("D"));     // Prints: "2533"

...または便利なワンライナーとして:

Console.WriteLine("{0:G} ({0:D})", dentist);  // Prints: "Dentist (2533)"

これは とConsole.WriteLine同様に で機能しString.Formatます。

于 2013-04-11T11:03:41.060 に答える
7

次のようなものが必要なようです。

// Please drop the "e" prefix...
OccupationCode code = OccupationCode.Dentist;

string text = string.Format("{0} ({1})", code, (int) code);
于 2013-04-11T10:57:59.413 に答える
2

What C# code would output the following for a variable of the enum type below?

キャストしないと、列挙型識別子が出力されます。Dentist

その列挙値にアクセスする必要がある場合は、キャストする必要があります。

int value = (int)eOccupationCode.Dentist;
于 2013-04-11T10:59:30.963 に答える
0

私はあなたがこれを意味すると思います

eOccupationCode code = eOccupationCode.Dentist;
Console.WriteLine(string.Format("{0} ({1})", code,(int)code));
// outputs Dentist (2533)
于 2013-04-11T11:00:13.357 に答える