[英]Is it possible to use Enum with Pair Values like dictionary
在c#中我很難理解Enum。
在我的特定情況下,我需要以名稱值格式存儲常量值,如>
300秒= 5分鍾
目前我使用這個課程。
你能給我一個代碼示例嗎?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyWebSite.Models
{
public class Reminders
{
private sortedDictionary<int, string> remindersValue = new SortedDictionary<int, string>();
// We are settign the default values using the Costructor
public Reminders()
{
remindersValue.Add(0, "None");
remindersValue.Add(300, "5 minutes before");
remindersValue.Add(900, "15 minutes before");
}
public SortedDictionary<int, string> GetValues()
{
return remindersValue;
}
}
}
您可以使用Tuple<int, int>
作為字典鍵(至少使用.NET> = 4)。
但由於您實際上想要存儲TimeSpan
,請將其用作密鑰。
private static Dictionary<TimeSpan, string> TimeSpanText = new Dictionary<TimeSpan, string>();
static Reminders()
{
TimeSpanText.Add(TimeSpan.Zero, "None");
TimeSpanText.Add(TimeSpan.FromMinutes( 5 ), "5 minutes before");
TimeSpanText.Add(TimeSpan.FromMinutes( 15 ), "15 minutes before");
TimeSpanText.Add(TimeSpan.FromMinutes( 30 ), "30 minutes before");
TimeSpanText.Add(TimeSpan.FromHours( 1 ), "1 hour before");
// ....
}
public static string DisplayName(TimeSpan ts)
{
string text;
if (TimeSpanText.TryGetValue(ts, out text))
return text;
else
throw new ArgumentException("Invalid Timespan", "ts");
}
您可以通過以下方式獲得翻譯:
var quarter = TimeSpan.FromMinutes(15);
string text = TimeSpanText[ quarter ];
您可以使用描述屬性修飾枚舉,然后通過反射訪問它們。 例如,
enum ReminderTimes
{
[Description("None")]
None = 0,
[Description("5 minutes before")]
FiveMinutesBefore = 300,
[Description("15 minutes before")]
FifteenMinutesBefore = 900
}
您可以通過以下方式獲取說明:
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
另見: http : //www.codeproject.com/Articles/13821/Adding-Descriptions-to-your-Enumerations
枚舉實際上是一個命名的整數類型。 例如
public enum Foo : int
{
SomeValue = 100,
}
這意味着您創建了一個類型為'int'的Foo枚舉和一些值。 我個人總是明確地說明發生了什么,但是c#隱式地使它成為'int'類型(32位int)。
您可以使用任何名稱作為枚舉名稱,並可以使用Enum.IsDefined檢查它是否是有效的枚舉(例如,檢查300是否是有效的枚舉名稱)。
更新
好吧,實際上,說實話,這並非100%正確。 此更新僅用於顯示實際發生的情況。 枚舉是一種值類型,其字段充當名稱。 例如,上面的枚舉實際上是:
public struct Foo
{
private int _value;
public static Foo SomeValue { get { return new Foo() { _value = 100 }; } }
}
請注意,'int'是int的類型(在我的情況下是顯式的)。 因為它是一個值類型,它具有與內存中的實數整數相同的結構 - 這可能是編譯時編譯器正在使用的內容。
如果你問你可以存儲一個整數值對enum,那么你可以,例如
public enum DurationSeconds
{
None = 0,
FiveMinutesBefore = 300,
FifteenMinutesBefore = 900,
ThirtyMinutesBefore = 1800,
OneHourBefore = 3600,
TwoHoursBefore = 7200,
OneDayBefore = 86400,
TwoDaysBefore = 172800
}
與我通常所做的相反,我將添加另一個答案,即IMO解決問題的答案。
在實際使用運行時檢查之前,通常希望編譯器盡可能多地進行檢查。 這意味着在這種情況下使用Enum獲取值:
// provides a strong type when using values in memory to make sure you don't enter incorrect values
public enum TimeSpanEnum : int
{
Minutes30 = 30,
Minutes60 = 60,
}
public class Reminders
{
static Reminders()
{
names.Add(TimeSpanEnum.Minutes30, "30 minutes");
names.Add(TimeSpanEnum.Minutes60, "60 minutes");
}
public Reminders(TimeSpanEnum ts)
{
if (!Enum.IsDefined(typeof(TimeSpanEnum), ts))
{
throw new Exception("Incorrect value given for time difference");
}
}
private TimeSpanEnum value;
private static Dictionary<TimeSpanEnum, string> names = new Dictionary<TimeSpanEnum, string>();
public TimeSpan Difference { get { return TimeSpan.FromSeconds((int)value); } }
public string Name { get { return names[value]; } }
}
在創建這樣的程序時,該語言可以通過以下幾種方式幫助您:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.