ok once you have set a session variable i was wondering wich is the preffered method used to cast it to say a textbox
for examples
tbCity.Text = (string)Session["City"];
or
tbCity.Text = Session["City"].ToString();
would like ot know wich is the preffered method and why?
ilphrin:
tbCity.Text = (string)Session["City"];
I prefer your first way because you are being explicit about what the type is. otherwise, you are assuming it is an object that implements ToString(). Actually, I'm surprised that even works. I did not know Object has a ToString().
I THINK, HERE IS A DIFFERENCE
tbCity.Text =Session["City"].ToString(); Copies the value Session["City"] has into tbCity.Text
tbCity.Text = (string)Session["City"]; Trys to cast Session["City"] into a string object and copy a pointer
ilphrin:
I prefer your first way because you are being explicit about what the type is. otherwise, you are assuming it is an object that implements ToString().
All objects in .NET implement ToString() so that is not the problem.
farooq.kaiser:
I THINK, HERE IS A DIFFERENCE
tbCity.Text =Session["City"].ToString(); Copies the value Session["City"] has into tbCity.Text
tbCity.Text = (string)Session["City"]; Trys to cast Session["City"] into a string object and copy a pointer
No, it's by value (not a pointer) in both cases.
The technical difference is that ToString() will call the (hopefully) overridden ToString method on the object in Session["City"], whereas the (string)version tries to cast the object (may fail) to a string.
Problems with ToString - The object may be null, and you will get an "object reference not set" exception.
Problem with cast - The object may not be castable to string
Alternative solution:
tbCity.Text = Convert.ToString(Session["City"]);
Convert will use ToString internally, but if Session["City"] is null, it will instead return an empty string (unlike the cast, which will return a null string)
I usually use the following syntax:
Session["ObjectList"] as ObjectListType;
The *as* keyword ensures that if the casting fails there will be no error but a null value is returned.
no i am just wondering wich will be a preffered way of doing it as in wich way uses less overhead, and perhaps is a tad bit faster to execute
the main thing is i can get around a null value for ToString by using an if (Session["City"] != null)
and then putting it into the textbox. basically it is what is the best choice in the area of overhead, could be using literally hundreds of these iterated thru and want the fastest execution.
Performance wise they are virtually identical (perhaps a small, small edge to ToString() but checking for null would most likely eat that difference). I'm quite sure you have other areas of optimization
which will give you differences that are magnitudes larger than the difference between these, even if you have tens of thousands of records.
I am agree withgunteman you should always use Convert.ToString(your object)
Convert provide a lot of method like ToInt etc. just have a look.
0 comments:
Post a Comment