[C#] Best method for storing "temporary" data/fields
Hi
Been looking for an alternative for storing temporary data, things that will only be used for a matter of time, then never used again etc. A method I've been looking for for a while is the Dictionary collection type. But reading that it's slow when getting indexes etc, i cannot make up my mind if i should just stick to normal variables, or go to collection storage.
Field:
Code:
public Dictionary<string, object> TemporaryAttributes = new Dictionary<string, object>();
Adding data:
Code:
TemporaryAttributes.Add("LOGIN_STAGE", 0);
use:
Code:
If (TemporaryAttributes["LOGIN_STAGE"] == 0) return;
If (TemporaryAttributes["LOGIN_STAGE"] == 1) //do some shiet//
TemporaryAttributes.Remove("LOGIN_STAGE");
etc etc
instead of:
Code:
int loginStage = 0;
...
if (loginStage == 0) return;
if (loginStage == 1) //do some shiet here//
Good idea?
Re: [C#] Best method for storing "temporary" data/fields
Give more information. What exactly is temporary data?
- How long does it need to last?
- What's the scope of the data? Method, class, global?
- Is it going to be accessed by multiple threads?
- How often will it need to be accessed?
- Why do you even want to change the way you store it?
Based on my assumptions
- It probably only needs to last until the end of the method.
- See above.
- Probably not.
- Probably not very often.
- A very good question, why do you want to change it?
Again, more information please.
Re: [C#] Best method for storing "temporary" data/fields
An example of what i'll be using it for would be login.
It would be used in inside a method, then removed at the end of the message, yes.
A hashmap (dictionary<string, object>) is created for every player (initialized @ constructor)
in login method a temp attribute is created, called login stage, after usage, it's removed from the hashmap.
How often will it be used? it's created for each player login
No it wont be used by multiple threads, only one.
no i won't change the way i store it, but it will change values
not accessed much,
Re: [C#] Best method for storing "temporary" data/fields
You're pretty well off using the dictionary., that is if you have a fair amount of "temporary data" you need to store. If it's really only two or three, using a dictionary might be a bit unnecessary, as creating a simple type to store the data might be a better alternative if you're just looking to cut down on the amount of variables contained in your class. If you're dealing with more than just a few (greater than say four or five), dictionary is probably the best choice.
Re: [C#] Best method for storing "temporary" data/fields
I think it's a bit better if i use a local variable, then use the ref key to change it in other methods if needed