tags:

views:

43

answers:

4

I have an ASP.Net app that retrieves a collection of roles for a given user. Each role is a string. There's rarely more than 2-3 roles for any one person.

I wanted to store the colelction of roles in Session state to save the time accessing the DB on each request.

I basically want to write code that checks if a given string exists in the collection to test if the user has that role or not.

My question is...what's the best way to do this? Is an array the best collection to store the role strings in Session with? Or is a dictionary better? I figure an array will take less memory (I don't really need a key + value) although a dictionary might lookup faster.

What would you suggest?

+2  A: 

Don't worry about optimization until its time to optimize. An array is fine. As far as determining if the string exists, I'd compare each element in the array using Equals using StringComparison.OrdinalIgnoreCase, as your roles probably will be culturally neutral.


"foo" == "bar" is an Ordinal comparison. Case matters. For roles, you usually don't care about case (is an 'administrator' the same thing as an 'Administrator'? In any rational world the answer is yes), so you want to compare using an Ordinal comparison that ignores case, or OrdinalIgnoreCase.

Will
Is there any speed difference between == and .equals("", StringComparison.OrdinalIgnoreCase) ?
Wackyphill
@Wackyphill: I noticed you are very specific about performance and memory usage. Not sure about your app, but in many cases, you probably can't feel the difference without benchmarking.
o.k.w
http://blogs.msdn.com/noahc/archive/2007/06/29/string-equals-performance-comparison.aspx
Will
A: 

A List<string> would probably be best. If you know for a fact that there will never be more than (say) 3, then a string array might be slightly better - probably not enough to make a difference. The hashing required for a dictionary is probably overkill for such a small list.

Ray
Why would a List<String> be better for larger collections?
Wackyphill
Mainly because you don't have to know the number of items up front as you do when you create an array - I don't suppose there is a significant performance difference when looking up items.
Ray
I will use a List<String> for when I originally query the DB for the roles since I don't know ahead of time how many there will be.Then I think I may save some memory by converting the List to an array and putting the Array in Session which I *think* might be more memory efficient.
Wackyphill
A: 

Something like this?

//string as a generic list
List<string> roles;

//check if list contains a certain role
roles.Contains(TheRoleToCheck);
o.k.w
Yes that's nice and clean. Do you know if a List<String> takes any more memory than a string[]?
Wackyphill
I can'tb e certain of memory usage but certainly easier to query then loop through the array, yea?
o.k.w
Well, you can do a `.Contains()` lookup on an array with LINQ, instead of looping yourself, so the syntax for an array or a List is identical.
Joel Mueller
@Joel: Good point :)
o.k.w
+1  A: 

A HashSet<string> technically has faster lookups than a List<string>. It's basically a Dictionary without any values. However, when we're talking about only 2-3 items, I don't think it matters much whether you use a List or a HashSet.

Joel Mueller
Hmm I wasn't aware of a HashSet. That's cool.
Wackyphill
Yeah my roles are soo small I'll use a List<string> for the DB query then connvert them to an array and store that. Thanks.
Wackyphill