जावा में मैं इस तरह के एनम मूल्यों या ओवरराइडिंग विधियों का विस्तार करता था:
enum SomeEnum
{
option1("sv")
{
public String toString()
{
return "Some value";
}
},
option2;
private String PassedValue;
public SomeEnum(String somevalue)
{
this.PassedValue = somevalue;
}
public SomeEnum()
{
this.PassedValue = "Default Value";
}
public String getPassedValue()
{
return this.PassedValue;
}
}
सी # में कुछ ऐसा करने का कोई तरीका है या सी # में अधिक सीमित हैं
4 जवाब
काश .NET में एनम अधिक शक्तिशाली होते। और मैं प्यार करता हूँ। नेट! आप एक ही चीज़ को पूरा करने के लिए विशेषताओं का उपयोग कर सकते हैं। नीचे दिए गए कोड को एक बार लिखें और हर जगह इसका इस्तेमाल करें। यह एक लंबा जवाब होगा लेकिन मुझे लगता है कि यह एक बहुत अच्छा समाधान है इसलिए धैर्य रखें!
प्रयोग
SomeEnum e = SomeEnum.ValueTwo;
string description = e.GetDescription();
Enum
Enum और उसके मूल्यों का वर्णन करने के लिए विशेषताओं का उपयोग करें।
[DescriptiveEnumEnforcement(DescriptiveEnumEnforcement.EnforcementTypeEnum.ThrowException)]
public enum SomeEnum
{
[Description("Value One")]
ValueOne,
[Description("Value Two")]
ValueTwo,
[Description("Value 3")]
ValueThree
}
विवरणविशेषता
/// <summary>Indicates that an enum value has a description.</summary>
[AttributeUsage(AttributeTargets.Field)]
public class DescriptionAttribute : System.Attribute
{
/// <summary>The description for the enum value.</summary>
public string Description { get; set; }
/// <summary>Constructs a new DescriptionAttribute.</summary>
public DescriptionAttribute() { }
/// <summary>Constructs a new DescriptionAttribute.</summary>
/// <param name="description">The initial value of the Description property.</param>
public DescriptionAttribute(string description)
{
this.Description = description;
}
/// <summary>Returns the Description property.</summary>
/// <returns>The Description property.</returns>
public override string ToString()
{
return this.Description;
}
}
वर्णनात्मकEnumEnforcementAttribute
यह सुनिश्चित करने के लिए एक विशेषता कि आपके एनम को ठीक से कॉन्फ़िगर किया गया है।
/// <summary>Indicates whether or not an enum must have a NameAttribute and a DescriptionAttribute.</summary>
[AttributeUsage(AttributeTargets.Enum)]
public class DescriptiveEnumEnforcementAttribute : System.Attribute
{
/// <summary>Defines the different types of enforcement for DescriptiveEnums.</summary>
public enum EnforcementTypeEnum
{
/// <summary>Indicates that the enum must have a NameAttribute and a DescriptionAttribute.</summary>
ThrowException,
/// <summary>Indicates that the enum does not have a NameAttribute and a DescriptionAttribute, the value will be used instead.</summary>
DefaultToValue
}
/// <summary>The enforcement type for this DescriptiveEnumEnforcementAttribute.</summary>
public EnforcementTypeEnum EnforcementType { get; set; }
/// <summary>Constructs a new DescriptiveEnumEnforcementAttribute.</summary>
public DescriptiveEnumEnforcementAttribute()
{
this.EnforcementType = EnforcementTypeEnum.DefaultToValue;
}
/// <summary>Constructs a new DescriptiveEnumEnforcementAttribute.</summary>
/// <param name="enforcementType">The initial value of the EnforcementType property.</param>
public DescriptiveEnumEnforcementAttribute(EnforcementTypeEnum enforcementType)
{
this.EnforcementType = enforcementType;
}
}
विवरण प्राप्त करना
/// <summary>Provides functionality to enhance enumerations.</summary>
public static partial class EnumUtil
{
/// <summary>Returns the description of the specified enum.</summary>
/// <param name="value">The value of the enum for which to return the description.</param>
/// <returns>A description of the enum, or the enum name if no description exists.</returns>
public static string GetDescription(this Enum value)
{
return GetEnumDescription(value);
}
/// <summary>Returns the description of the specified enum.</summary>
/// <param name="value">The value of the enum for which to return the description.</param>
/// <returns>A description of the enum, or the enum name if no description exists.</returns>
public static string GetDescription<T>(object value)
{
return GetEnumDescription(value);
}
/// <summary>Returns the description of the specified enum.</summary>
/// <param name="value">The value of the enum for which to return the description.</param>
/// <returns>A description of the enum, or the enum name if no description exists.</returns>
public static string GetEnumDescription(object value)
{
if (value == null)
return null;
Type type = value.GetType();
//Make sure the object is an enum.
if (!type.IsEnum)
throw new ApplicationException("Value parameter must be an enum.");
FieldInfo fieldInfo = type.GetField(value.ToString());
object[] descriptionAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
//If no DescriptionAttribute exists for this enum value, check the DescriptiveEnumEnforcementAttribute and decide how to proceed.
if (descriptionAttributes == null || descriptionAttributes.Length == 0)
{
object[] enforcementAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptiveEnumEnforcementAttribute), false);
//If a DescriptiveEnumEnforcementAttribute exists, either throw an exception or return the name of the enum instead.
if (enforcementAttributes != null && enforcementAttributes.Length == 1)
{
DescriptiveEnumEnforcementAttribute enforcementAttribute = (DescriptiveEnumEnforcementAttribute)enforcementAttributes[0];
if (enforcementAttribute.EnforcementType == DescriptiveEnumEnforcementAttribute.EnforcementTypeEnum.ThrowException)
throw new ApplicationException("No Description attributes exist in enforced enum of type '" + type.Name + "', value '" + value.ToString() + "'.");
return GetEnumName(value);
}
else //Just return the name of the enum.
return GetEnumName(value);
}
else if (descriptionAttributes.Length > 1)
throw new ApplicationException("Too many Description attributes exist in enum of type '" + type.Name + "', value '" + value.ToString() + "'.");
//Return the value of the DescriptionAttribute.
return descriptionAttributes[0].ToString();
}
}
T
के सभी मूल्यों पर एक सामान्य गणना करने के लिए संघर्ष कर रहा था और विवरण प्राप्त करने से पता चला कि मेरी कास्ट को एनम में गलत था। यदि e
आपका एनम मान int
है और enumType
एनम का Type
है, तो आप (Enum.ToObject(enumType, e) as Enum).GetDescription()
के साथ विवरण प्राप्त कर सकते हैं
सी # में एनम्स केवल (पूर्णांक) मानों के लिए हैं; उनके पास जावा की तरह विशेष तरीके या कंस्ट्रक्टर नहीं हो सकते।
हालांकि, आप विस्तार विधियों को परिभाषित कर सकते हैं जो लगभग समान प्रभाव प्राप्त करने के लिए एनम पर काम करती हैं:
public enum MyEnum {
Foo = 1,
Bar = 2,
Default = Foo
}
public static class MyEnumExtensions
{
public static Widget ToWidget(this MyEnum enumValue) {
switch (enumValue) {
case MyEnum.Foo:
return new Widget("Foo!");
case MyEnum.Bar:
return new Widget("Bar...");
default:
return null;
}
}
}
तब आप कह सकते थे:
var val = MyEnum.Foo;
var widget = val.ToWidget();
सी # में एनम मूल रूप से केवल प्राइमेटिव नामित हैं। वे आम तौर पर int
पर आधारित होते हैं, लेकिन किसी भी संख्यात्मक आदिम पर आधारित हो सकते हैं। तो सी # लगभग उस कार्यक्षमता की पेशकश नहीं करता है जो जावा एनम करते हैं। व्यापार बंद है सी # एनम बहुत हल्का है, जबकि जावा एनम पूर्ण विकसित वस्तुएं हैं।
public enum FooBar : int {
Foo = 1,
Bar = 2
}
उपरोक्त एनम एक int
से बहुत अलग नहीं है, सिवाय इसके कि अब हम शाब्दिक 1
के बजाय FooBar.Foo
का उपयोग कर सकते हैं। आप इनट्स के बीच आगे और पीछे एनम डाल सकते हैं, और Enum
में कुछ सहायक कार्य हैं जो एनम के साथ काम करते समय मदद कर सकते हैं। लेकिन यह इसके बारे में सी # के लिए है, वे सी या सी ++ एनम्स की तरह हैं।
आप एक्सटेंशन विधियों का उपयोग करके सी # में कुछ ऐसा ही कर सकते हैं।
enum Test
{
Value1,
Value2,
Value3
}
static class TextExtensions
{
public static string Value(this Test value)
{
string stringValue = default(String);
switch (value)
{
case Test.Value1:
{
stringValue = "some value 1";
} break;
case Test.Value2:
{
stringValue = "some value 2";
}; break;
case Test.Value3:
{
stringValue = "some value 3";
}; break;
}
return stringValue;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write(Test.Value1.Value());
Console.ReadLine();
}
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
c#
C # (उच्चारण "तेज देखें") Microsoft द्वारा विकसित एक उच्च स्तरीय, सांख्यिकीय रूप से टाइप किया हुआ, बहु-प्रतिमान प्रोग्रामिंग भाषा है। C # कोड आमतौर पर Microsoft के .NET परिवार के टूल और रन-टाइम को लक्षित करता है, जिसमें .NET फ्रेमवर्क, .NET कोर और Xamarin अन्य शामिल हैं। C # या C # के औपचारिक विनिर्देश में लिखे गए कोड के बारे में प्रश्नों के लिए इस टैग का उपयोग करें।