c# - Does Creating a Variable to hold a cast Save Overhead -
when need save big list of custom objects between postbacks on asp.net project, save them in session variable, cast them list<> in property this
public partial class mypage : page { private list<mystorageobject> mylist { {return (list<mystorageobject>) session["mysessionstring"]; } set { session["mysessionstring"] = value; } } }
then put list of objects in there, , call in later postback. think pretty standard way make objects last longer page lifecycle.
but question is, when i'm using list more once, i'm going few things it, can't think every time access it, it's casting object list<> if create variable looks it's avoiding casting.
private void dostuffwithmylist() { var x = mylist; //assuming i'm going call list 20 times in method x.first(); // more efficient / cleaner mylist.first(); // this? //or both converted same thing in end anyway }
yes, in first case (using x
) you're accessing property once , reusing returned value.
in second case (using mylist
multiple times) you'll looking value in session every time use it. it's casting, that's likely cheaper session lookup.
fundamentally i'd regard first version cleaner, makes clearer you're trying reuse same value several times.
Comments
Post a Comment