草庐IT

truth_enum

全部标签

c# - 如何在不装箱的情况下比较 System.Enum 和枚举(实现)?

如何在不装箱的情况下将System.Enum与enum进行比较?例如,如何在不装箱enum的情况下使以下代码工作?enumColor{Red,Green,Blue}...System.EnummyEnum=GetEnum();//ReturnsaSystem.Enum.//MaybeaColor,maybesomeotherenumtype....if(myEnum==Color.Red)//ERROR!{DoSomething();}具体而言,此处的目的不是比较基础值。在这种情况下,基本值(value)并不重要。相反,如果两个枚举具有相同的基础值,如果它们是两种不同类型的枚举,则不应

c# - DDD : Enum like entities

我有以下数据库模型:**Persontable**ID|Name|StateId------------------------------1Joe12Peter13John2**Statetable**ID|Desc------------------------------1Working2Vacation领域模型将是(简化的):publicclassPerson{publicintId{get;}publicstringName{get;set;}publicStateState{get;set;}}publicclassState{privateintid;publicstri

c# - Enum.ToString() 已弃用?

当我在VisualStudio中的Enum类型上键入.ToString()时,Intellisense显示通过ToString()(虽然它构建并工作正常)。这似乎表明Enum.ToString()在某种程度上已被弃用。这是真的?如果是,为什么? 最佳答案 采用IFormatProvider的Enum.ToString重载,ToString(IFormatProvider)和ToString(String,IFormatProvider),都已过时,因为无论如何都没有使用IFormatProvider。其他重载,ToString()和

c# - 公共(public)静态字符串 MyFunc() 上的 "Expected class, delegate, enum, interface or struct"错误。什么 's an alternative to "字符串”?

当我尝试使用以下静态函数时出现错误。错误:Expectedclass,delegate,enum,interface,orstruct函数(和类):namespaceMyNamespace{publicclassMyClass{//SomeotherstaticmethodsthatuseClasses,delegates,enums,interfaces,orstructspublicstaticstringMyFunc(stringmyVar){stringmyText=myVar;//DosomestuffwithmyTextandmyVarreturnmyText;}}}这导致

c# - DataAnnotations 命名空间中的 Enum 值是否有开箱即用的验证器?

C#枚举值不仅限于其定义中列出的值,还可以存储其基类型的任何值。如果未定义基类型,则使用Int32或仅使用int。我正在开发一个WCF服务,它需要确信某些枚举已经分配了一个值,而不是所有枚举的默认值0。我从一个单元测试开始,以确定[Required]会在这里做正确的工作。usingSystem.ComponentModel.DataAnnotations;usingXunit;publicenumMyEnum{//Ialwaysstartfrom1inordertodistinctfirstvaluefromthedefaultvalueFirst=1,Second,}publiccl

c# - 为什么 Enum.Parse() 返回对象?

这里有很多关于将字符串转换为枚举值的问题。通常,答案看起来类似于thisquestion上的答案。:StatusEnumMyStatus=(StatusEnum)Enum.Parse(typeof(StatusEnum),"Active",true);虽然这是一个非常合理的答案,并且您可以编写一个方法来简化调用,但它没有回答为什么Enum.Parse()返回object的问题。而不是适当的枚举值。为什么我必须将它转换为StatusEnum?编辑:基本上,问题是为什么像这样的函数不是Enum类的一部分?publicstaticTParse(stringvalue)whereT:struc

c# - 为什么 Enum 的 HasFlag 方法需要装箱?

我正在阅读“C#viaCLR”,在第380页上,有一条说明如下:NoteTheEnumclassdefinesaHasFlagmethoddefinedasfollowspublicBooleanHasFlag(Enumflag);Usingthismethod,youcouldrewritethecalltoConsole.WriteLinelikethis:Console.WriteLine("Is{0}hidden?{1}",file,attributes.HasFlag(FileAttributes.Hidden));However,Irecommendthatyouavoid

c# - Enum.GetNames() 导致带有负枚举常量的意外顺序

我有以下枚举定义(在C#中):publicenumELogLevel{General=-1,//Shouldonlybeusedindrop-downboxinMerliniaAdministratorlogsettingsAll=0,//Shouldnotbeusedasalevel,onlyasathreshold,effectivelysameasTraceTrace=1,Debug=2,Info=3,Warn=4,Error=5,Fatal=6,Off=7//Shouldnotbeusedasalevel,onlyasathreshold}现在,当我对这种类型执行Enum.Ge

c# - Array 类和 Enum.GetValues() 的简单形式

我正在使用静态方法Enum.GetValues(typeof(SomeEnum));当您需要做的只是枚举值时,此方法非常有用,但出于某种原因,它返回一个非常简单的Array类形式。我正在尝试找到一种简单的方法将其返回值转换为更“正常”的集合类,例如常规数组或List。到目前为止,如果我想这样做,我必须枚举Enum.GetValues(typeof(SomeEnum));的输出。并将它们一一添加到列表。有什么想法可以更干净地做到这一点吗?答案:关键是对返回结果进行强制转换--SomeEnum[]enums=(SomeEnum[])Enum.GetValues(typeof(SomeEnu

c# - : Dictionary<string, object> 或 Dictionary<enum,object> 哪个更快/更有效?

enum类型用作字典键时是否比string类型更快/更有效?IDictionaryorIDictionary事实上,哪种数据类型最适合作为字典键,为什么?请考虑以下事项:注意:为简单起见,只有5个属性structMyKeys{publicstringIncomplete="IN";publicstringSubmitted="SU";publicstringProcessing="PR";publicstringCompleted="CO";publicstringClosed="CL";}和enumMyKeys{Incomplete,Submitted,Processing,Comp