I have a function that assigns values to a dictionary created in c#.
After some computations in the function, the dictionary is compsed of a value and a key.
I need to create a session save this dictionary in this session.
Can anybody help?
I have a function that assigns values to a dictionary created in c#. After
Share
Put simply, if you have a
Dictionary<string, List<int>>then putting it in the session is not very complicated;If you want to check if the Session already contains your dictionary, and only add it if it doesn’t then:
To get it out, remember that it’s not typed,
Sessionis effectively a dictionary of string, object so you’ll have to cast:However, you should also consider what the lifecycle of this object is going to be – Session is not stable in that it has a finite lifetime – it will disappear after a predetermined time of inactivity, for example, so if you want it to hang around for a longer time then you may need to consider alternative strategies.
There’s a ton of documentation on Session over at MSDN and it’s also a very common topic on Stack Overflow.
Remember also that, as Session is effectively a
Dictionary<String, Object>managed by the .Net Framework and IIS, you could also do:And, of course, on retrieval:
Other alternatives include the Cache; if your object is going to be available to all users of the application then this might be a better solution. Please see this answer for more information.
I’ve also voted to close your question as I believe it’s too localized, but I hope my answer helps you along the way.