iT邦幫忙

0

C# Enum如何透過Name去取得Value?

  • 分享至 

  • xImage

C# Enum如何透過Name去取得Value
小弟在 Enum 內宣告了不少東西

        public enum enum_Channel
        {
            Ch01 = 0x81,
            Ch02,
            Ch03,
            Ch04,
            Ch05,
            Ch06,
            Ch07,
            Ch08,
            Ch09,
            Ch10,
            Ch11,
            Ch12,
            Ch13,
            Ch14,
            Ch15,
            Ch16,
            Ch17,
            Ch18,
            Ch19,
            Ch20,
            Ch21,
            Ch22,
            Ch23,
            Ch24,
            Ch25,
            Ch26,
            Ch27,
            Ch28,
            Ch29,
            Ch30,
            Ch31,
            Ch32,
            Ch33,
            Ch34,
            Ch35,
            Ch36,
            Ch37,
            Ch38,
            Ch39,
        }

但是我發現如果我要直接把這些東西的內容值取出來,並且印出來,好像辦不到
比方我我想印出Ch39的數值(167),該怎麼印呢?

Console.WriteLine( enum_Channel.Ch01.ToString() );

.............剛剛看到海大大回了同樣的答案...........不敢刪.....
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
海綿寶寶
iT邦大神 1 級 ‧ 2020-05-03 17:18:01
最佳解答
using System;
					
public class Program
{
	public enum enum_Channel
        {
            Ch01 = 0x81,
            Ch02,
            Ch03,
            Ch04,
            Ch05,
            Ch06,
            Ch07,
            Ch08,
            Ch09,
            Ch10,
            Ch11,
            Ch12,
            Ch13,
            Ch14,
            Ch15,
            Ch16,
            Ch17,
            Ch18,
            Ch19,
            Ch20,
            Ch21,
            Ch22,
            Ch23,
            Ch24,
            Ch25,
            Ch26,
            Ch27,
            Ch28,
            Ch29,
            Ch30,
            Ch31,
            Ch32,
            Ch33,
            Ch34,
            Ch35,
            Ch36,
            Ch37,
            Ch38,
            Ch39,
        }
	public static void Main()
	{
		Console.WriteLine((int)enum_Channel.Ch39);
	}
}
lazywayne iT邦新手 5 級 ‧ 2020-05-04 10:11:17 檢舉

我早該想到了/images/emoticon/emoticon06.gif

我要發表回答

立即登入回答