More elegant listing design

I am learning C #.

I heard that C # is one of the most constructive languages. so would you guys make my code more elegant and efficient?

public class ISO639 { public enum ISO639Code { Afrikaans, //af Albanian, //sq Amharic, //am ... Yiddish, //yi Unknown } public static string GetISO639CodeString(ISO639.ISO639Code l) { switch (l) { case ISO639Code.English: return "en"; case ISO639Code.Japanese: return "ja"; ... case ISO639Code.Hebrew: return "he"; default: return ""; } public static ISO639.ISO639Code GetISO39CodeValue(string s) { switch (s) { case "ko" : return ISO639Code.Korean; case "en" : return ISO639Code.English; ... case "hu" : return ISO639Code.Hungarian; default: return ISO639Code.Unknown; } } } 

Here is my ISO639 class. This class provides an enumeration for the ISO639 code, but I need a type conversion from the ISO639 list to a regular string. (e.g. ISO639.ISO639Code.Italian => "it"). I also need a type conversion from a simple string to ISO639 encoding. (e.g. "it" => ISO639.ISO639Code.Italian).

Is there a more efficient coding style for this?

+7
source share
9 answers

You can add the standard System.ComponentModel.Description attribute for each enumeration entry, and then read it.

 public enum ISO639Code { [Description("af")] Afrikaans } public static class EnumExtensions { // Extension method to read Description value public static string GetDescription(this Enum currentEnum) { var fi = currentEnum.GetType().GetField(currentEnum.ToString()); var da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); return da != null ? da.Description : currentEnum.ToString(); } } // **How-to read it** ISO639Code isoCode = ISO639Code.Afrikaans; // this will returns "af" string isoDescription = isoCode.GetDescription(); 

EDIT:

  string searchFor = "af"; ISO639Code foundEntry; // Loop through all entries descriptions var allEntries = Enum.GetValues(typeof(ISO639Code)); foreach (var entry in allEntries) { // If you will extract this as separate method and use it for search not only loop // through the all entries - you can put here is yield return description var currentEntry = ((ISO639Code)entry); string description = currentEntry.GetDescription(); if (description == searchFor) { foundEntry = currentEntry; break; } } 
+9
source

Of course. You can use attributes:

 public enum ISO639Code { [CodeString("af")] Afrikaans, [CodeString("sq")] Albanian, } 
+8
source

I suggest you use the C # extension methods for listing, they let you add whatever logic you want.

For example, see http://pietschsoft.com/post/2008/07/c-enhance-enums-using-extension-methods.aspx

+2
source

Use a dictionary, for example: new Dictionary<ISO639Code, string> .

+2
source

I would just save the information in a dictionary-like object. Thus, you can refer to the name by key and get the value directly.

+1
source

You have an enumeration:

  public enum ISO639Code { Afrikaans = 1, Albanian = 2, Amharic = 3, 

and etc.

Create database table:

  ISO639Id int PK, ISO639Code char(2) 

If ISO639Id matches the enumeration value.

In the code, you need the ISO630 class, which contains identifier and code values ​​read from the database. (You can load this once and then cache it in memory.)

The beauty of this approach is that it can be easily extended, so if in the future you want to store more information for each ISO639 code, you can simply add another field.

0
source

Take a look at the System.Globailzation namespace. The necessary functions look already implemented there. In the worst case, you can see the architecture and technology used in the .Net infrastructure to solve a very similar problem.

0
source

Enumerations are really good for working in code, since they are really strongly typed and make refactoring easier. Follow these steps:

  • Use the attributes for any additional information that you want to attach to the listing. This is usually a simple Description attribute. Something like:

    public enum IsoCodes {[Description ("AF")] Africans = 0, [Description ("I")] Americans = 1}

Then write some extension methods to convert strings and integers to and from this listing:

 public static string GetDescription(this Enum value) { var entries = value.ToString().Split(FlagEnumSeparatorCharacter); var description = new string[entries.Length]; for (var i = 0; i < entries.Length; i++) { var fieldInfo = value.GetType().GetField(entries[i].Trim()); var attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; description[i] = (attributes.Length > 0) ? attributes[0].Description : entries[i].Trim(); } return String.Join(", ", description); } public static int GetValue(this Enum value) { return (int)value.GetType().GetField(value.ToString()).GetRawConstantValue(); } public static T ToEnum<T>(this string value) { if (typeof(T).BaseType.Name != typeof(Enum).Name) { throw new Exception("Not an enum"); } return (T)Enum.Parse(typeof(T), value, true); } public static T ToEnum<T>(this int value) { if (typeof(T).BaseType.Name != typeof(Enum).Name) { throw new Exception("Not an enum"); } return (T)Enum.ToObject(typeof(T), value); } 

Now use your listings as you like.

0
source

I would like to have ISO639Code as a class instead of enum:

 public class ISO639Code { public string Value { get; set ; } public string Code { get; set; } public ISO639Code() { this.Value = ""; this.Code = ""; } public ISO639Code(string value, string code) : this() { this.Value = value; this.Code = code; } public override bool Equals(object obj) { if (obj != null) { if (obj is string) return obj.ToString().Equals(this.Value, StringComparison.CurrentCultureIgnoreCase); if (obj is ISO639Code) return ((ISO639Code)obj).Value.Equals(this.Value, StringComparison.CurrentCultureIgnoreCase); } return false; } public override int GetHashCode() { return this.Value.GetHashCode(); } public override string ToString() { return this.Value; } } 

Then global List<ISO639Code> with all possible codes and find a specific code based on the code name or value, just search the list.

Personally, I prefer this over setting up an enumeration.

0
source

All Articles