At times when we access Session["key"].ToString() it gives exception when session is expired & in coding we try to access session variable. So I’m trying to create extension method on object class so that I could write it as Session["key"].getString() in coding so that every time I don’t have to do Convert.ToString(session["key"])
Any other solutions are also appreciable.
Just use the null-coalescing operator:
Update
If you must have a method to do this (extension or otherwise), I would do something like:
I might even go so far as to make it generic for other possible types with a
GetValuecall that takes a selector and then use lambdas:You would then use as follows: