c# - A btter way to represent Same value given multiple values(C#3.0) -
i have situation looking more elegant solution.
consider below cases
"bkp","bkp","book-to-price" (will represent) book-to-price "aop","aspect oriented program" (will represent) aspect-oriented-program
i.e. if user enter bkp or bkp or book-to-price , program should treat book-to-price. same holds second example(aspect-oriented-program).
i have below solution:
solution:
if (str == "bkp" || str == "bkp" || str == "book-to-price" ) return "book-to-price".
but think there can many other better solutions .
could people please give suggestion.(with example better)
i using c#3.0 , dotnet framework 3.5
i consider using dictionary<string,string>
, perhaps populated configuration file or database table, alias canonical name relationships. use case insensitive key comparer. 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 need, given, key o(1) lookup find canonical name.
var name = map["bkp"];
Comments
Post a Comment