Looking for a syntactic shortcut for accessing dictionaries
Posted
by Sisiutl
on Stack Overflow
See other posts from Stack Overflow
or by Sisiutl
Published on 2010-03-16T16:56:18Z
Indexed on
2010/03/17
9:21 UTC
Read the original article
Hit count: 269
c#
|dictionary
I have an abstract base class that holds a Dictionary. I'd like inherited classes to be able to access the dictionary fields using a convenient syntax. Currently I have lots of code like this:
string temp;
int val;
if (this.Fields.TryGetValue("Key", out temp)) {
if (int.TryParse(temp, out val)) {
// do something with val...
}
}
Obviously I can wrap this in utility functions but I'd like to have a cool, convenient syntax for accessing the dictionary fields where I can simply say something like:
int result = @Key;
Is there any way to do something like this in C# (3.5)?
© Stack Overflow or respective owner