开发者

What is the best way to store pairs of strings, make an object or use a class in .NET?

开发者 https://www.devze.com 2023-03-28 23:14 出处:网络
Don\'t know whether I\'m having a "thick day" - but I just wondered what is the best route here.

Don't know whether I'm having a "thick day" - but I just wondered what is the best route here.

Context:

I have a list of fields and I want to store alias names with them (I'm usin开发者_如何学Gog .NET 2.0 BTW) e.g.

What is the best way to store pairs of strings, make an object or use a class in .NET?

So its essentially a pair of strings:

REFERENCE, Ref

COMMUNITY, Community

POST_CODE, Zip Code

... and I'm thinking it seems overboard all the time to keep creating objects for things like this, so should I create a StringDictionary and store the values that way, even though I would not use any of the functionality of the StringDictionary class and I'm not bothered about a key value pair association etc - I just want to keep a pair of strings essentially.

Any help/pointers would be great.


The "pair" generic class for .NET is Tuple. You would use it like:

var strings=new List<Tuple<string, string>>();
strings.Add(Tuple.Create("REFERENCE", "Ref"));

A dictionary is a perfectly acceptable substitute if the left-most string is unique (ie a key). You'll get errors otherwise.

As to whether it's better to use the built-in collections or create an actual object, depends on your needs (will you be adding more columns later on? you can't do that with a dictionary approach), how often you use it (if it's a core type, you should probably make a domain model for it) etc.

Edit: As to not using any dictionary built-in functionality, that's not true: you're using its binary search algorithm and internal tree construction for lightning-fast look-ups. A list of either Tuple or your own type most likely won't have this and it will revert to a linear search.


How about System.Tuple?


You can use the generic Dictionary<string, string> if your "Field Names" are unique.

Otherwise you could use the Lookup class if you don't mind duplicate keys.

I wouldn't worry too much about whether you use the full functionality of those classes or not. I think the more important concern should be to write simple, easy to read & maintain code.


what about Tuple<string,string>? It's build into .net 4.0 and it's lightweight.


In my opinion, it is better to use a custom structure for storing two strings, because: System.Collections.Generic.Dictionary and System.Collections.Specialized.NameValueCollection require the method Add to add strings into them – it's additional lines of code. The class System.Tuple has only ReadOnly properties – this can be an obstacle.

Example of simple structure:

public struct DoppelWert
{
 public string Wert1; 
 public string Wert2;
}


For pairs of strings alternatively use NameValueCollection


Surprised, no one mentioned KeyValuePair? https://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx

Please note that if you are concerned about performance please have a look at this comparison http://www.dotnetperls.com/keyvaluepair

0

精彩评论

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

关注公众号