开发者

Can I use dot notation for accessing dictionary values?

I'm using Dictionary<string, string> as configuration for instruments, and it'd be easier for my users who don't know a lot about programming to be able to get autocomplete from Visual Studio.

In Python I can make a Dictionary and access the different values with a dot operator.

d = {'name':'Joe', 'mood':'grumpy'}
d.name
d.mood

Does C# have a way to do this?

I realize all of the problems involved since dictionary is a just a generic collection (how generic? is it just a list of KeyValuePairs? interesting question). I'm not about to write a wrapper class for this to accomplish it (I'd like it to be a bit more flexib开发者_Go百科le than using explicit properties with a custom class).


You can already do that in C# with anonymous types:

var d = new { name = "Joe", mood = "grumpy"};
d.name
d.mood

Or real types:

var d = new Person { Name = "Joe", Mood = "grumpy"};
d.Name
d.Mood

And, in C# 4.0 we will have the DLR which will allow adding properties at runtime using the ExpandoObject:

dynamic d = new ExpandoObject();
d.name = "Joe";
d.mood = "grumpy";

However, I'm not sure that you can do IntelliSense using static analysis in C#.

The closest C# equivalent to what you have is

var d = new Dictionary<string, string>()
    { { "name", "Joe" }, { "mood", "grumpy" } };
d["name"]
d["mood"]

Which will not support IntelliSense.

If you are making an API, I would use real types.


Seems like you want a struct. http://msdn.microsoft.com/en-us/library/0taef578.aspx

Edit: changed to a more up-to-date link.


you are looking for imo method chaining.

Method chaining - why is it a good practice, or not?

a dictionary object is a collection (uses list) of key value pairs that also implements a hash table for access. It is almost n(1) in terms of its access speed.

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, 
    ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, 
    IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

taken from

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/589059d3-d7f8-e09b-705d-91a461971cc2.htm

In fact the retrieval algorithm changes as it gets larger as a hash table is less efficient for small numbers of items.

The linq approach would work also - no need to use anonymous types though

moodyPerson p = new moodyPerson{mood = "good", name="jim"};

however this does not force you to set all the properties of a moody person and will use its empty constructor.

you cant use intellisense if you want to access properties by strings. You would need to use reflection to implement this.


Don't get suckered into an elaborate solution with ExpandoObject() if you are planning to instantiate your objects at runtime (like from a database call), there is no intellisense for "dot notation"and it will produce a compile error if you try to use it. you can use ["propname"] bracket notation to access them however at this point you might as well have created custom class objects or just manipulate the values as needed in a DataTable()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜