Question on code optimization

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I have a method is which I am accessing a session variable several times and
every time I retrieve it from session using Session["Variable"].ToString().

Is it advisable to retrieve this session variable and store it in a string
variable and access that string variable instead of session OR can I keep
following the way I have been accessing using Session["Variable"].ToString()?

Which one is an expensive operation?

Thanks for all ur pointers!!
 
If its on the same page, and you need it multi times,,,

read it once and set it to a local variable.

That way you only pay the unboxing price once.
 
I have personally used System.Convert.ToString() against Session
variables. I'm not sure if it's identical to using the .ToString()
method against an object, but it works for me. But to comment on your
question, in terms of performance expense, I'd have to agree with
Sloan. Unboxing once is much better then unboxing multiple times.

Perhaps if you're using a class, it would be easier to make a private/
public variable within that class and within the constructor, grab the
Session variable and assign it to that variable and just use the
object's method against the member variable. Just a thought from a
performance perspective.
 
Back
Top