开发者

A btter way to represent Same value given multiple values(C#3.0)

I have a situation for which I am looking for a more elegant solution.

Consider the below cases

"BKP","bkp","book-to-price" (will represent) BOOK-TO-PRICE

"aop","aspect oriented program" (will represent) ASPECT-ORIENTED-PROGRAM

i.e. if the user enter BKP or bkp or book-to-price , the program should treat that as BOOK-TO-PRICE. The same holds good for the second example(ASPECT-ORIENTED-PROGRAM).

I have the below solution:

Solution:

if (str == "BKP" || str == "bkp" || str == "book-to-price" ) return "BOOK-TO-PRICE".

But I think that there can be many other better solutions .

Could you people 开发者_运维技巧please give some suggestion.(with an example will be better)

I am using C#3.0 and dotnet framework 3.5


I would consider using a Dictionary<string,string>, perhaps populated from a configuration file or database table, with alias to canonical name relationships. Use a case insensitive key comparer. For example,

var map = new Dictionary<string,string>( StringComparer.OrdinalIgnoreCase );
map.Add( "bkp", "BOOK-TO-PRICE" );
map.Add( "book-to-price", "BOOK-TO-PRICE" );
map.Add( "aop", "ASPECT-ORIENTED-PROGRAM" );
map.Add( "aspect oriented program", "ASPECT-ORIENTED-PROGRAM" );

Then you only need, given, the key do an O(1) lookup to find the canonical name.

var name = map["bkp"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜