What is the difference between using the two following statements? It appears to me that the first “as string” is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight if any.
Page.Theme = Session["SessionTheme"] as string;
Page.Theme = Session["SessionTheme"].ToString();
If
Session["SessionTheme"]is not astring,as stringwill returnnull..ToString()will try to convert any other type to string by calling the object’sToString()method. For most built-in types this will return the object converted to a string, but for custom types without a specific.ToString()method, it will return the name of the type of the object.Another important thing to keep in mind is that if the object is
null, calling.ToString()will throw an exception, butas stringwill simply returnnull.