The difference between .ToString and "as a string" in C #

What is the difference between using the following two statements? It seems to me that the first β€œlike a string” is of type, while the second ToString is the actual method call that converts the input to a string? Just look for some kind of understanding, if any.

Page.Theme = Session["SessionTheme"] as string; Page.Theme = Session["SessionTheme"].ToString(); 
+64
source share
9 answers

If Session["SessionTheme"] not a string , as string will return null .

.ToString() will try to convert any other type to a string by calling the ToString() method of the object. For most built-in types, this will return an object converted to a string, but for custom types without a special .ToString() method, it will return the type name of the object.

 object o1 = "somestring"; object o2 = 1; object o3 = new object(); object o4 = null; string s = o1 as string; // returns "somestring" string s = o1.ToString(); // returns "somestring" string s = o2 as string; // returns null string s = o2.ToString(); // returns "1" string s = o3 as string; // returns null string s = o3.ToString(); // returns "System.Object" string s = o4 as string; // returns null string s = o4.ToString(); // throws NullReferenceException 

Another important thing to keep in mind is that if the object is null , calling .ToString() will throw an exception, but as string will just return null .

+94
source

The as keyword will basically check if the is object is instance of the type using the MSIL isinst under the hood. If so, it returns a reference to the object, otherwise a null reference.

He, as many say, is not trying to perform the translation as such, which involves some sort of exception handling. Not this way.

ToString() simply calls the object's ToString() method, either custom, implemented by the class (which, for most built-in types, converts to a string) - or if none are provided, the base class object one that returns the type of information.

+13
source
 Page.Theme = Session["SessionTheme"] as string; 

trying to pass a string

then

 Page.Theme = Session["SessionTheme"].ToString; 

calls the tostring method, which may be something really. This method does not use, it should return a string representation of this object.

+5
source

First of all, β€œ any object as a string ” and β€œ any-object.ToString () ” are completely different things in terms of their respective context.

 string str = any-object as string; 

1) This will cause any object to look like a string type, and if any object is not flushed to a string, then this operator will return null without throwing an exception. 2) This is a compiler service.
3) This works very well for any type other than a string, for example: you can do it like any object like Employee, where Employee is the class defined in your library.

 string str = any-object.ToString(); 

1) This will call ToString () of any object of type protection. Because System.Object defines the ToString () method, any class in the .NET framework has the ToString () method available for reinstallation. The programmer will override ToString () in an object class or structuring and write code that returns a suitable string representation of any object in accordance with the responsibility and role that an object plays.
2) Similar to how you can define an Employee and Over-ride ToString () method, which can return the string representation of an Employee object as "FIRSTNAME - LASTNAME, EMP-CDOE".

Please note that in this case, the programmer has control over ToString (), and he has nothing to do with casting or type conversion.

+4
source

To confuse the issue, C # 6.0 introduced an operator with a null condition . So, now it can also be written as:

 Page.Theme = Session["SessionTheme"]?.ToString(); 

which will return either null or the result from ToString () without causing an exception.

+3
source

I expand that Philip Leibert agreed a bit with the answer, because although I found resources comparing the three of them, I still could not find an explanation comparing all four.

  • (string)obj
  • obj as string
  • obj.ToString()
  • Convert.ToString(obj)
 object o1 = "somestring"; object o2 = 1; object o3 = new object(); object o4 = null; Console.WriteLine((string)o1); // returns "somestring" Console.WriteLine(o1 as string); // returns "somestring" Console.WriteLine(o1.ToString()); // returns "somestring" Console.WriteLine(Convert.ToString(o1)); // returns "somestring" Console.WriteLine((string)o2); // throws System.InvalidCastException Console.WriteLine(o2 as string); // returns null Console.WriteLine(o2.ToString()); // returns "1" Console.WriteLine(Convert.ToString(o2)); // returns "1" Console.WriteLine((string)o3); // throws System.InvalidCastException Console.WriteLine(o3 as string); // returns null Console.WriteLine(o3.ToString()); // returns "System.Object" Console.WriteLine(Convert.ToString(o3)); // returns "System.Object" Console.WriteLine((string)o4); // returns null Console.WriteLine(o4 as string); // returns null Console.WriteLine(o4.ToString()); // throws System.NullReferenceException Console.WriteLine(Convert.ToString(o4)); // returns "" 

It can be seen from these results that (string)obj and obj as string behave the same when obj is a string or zero; otherwise (string)obj throw an invalid cast exception, and obj as string will simply return zero. obj.ToString() and Convert.ToString(obj) also behave the same as others, except when obj is null, in which case obj.ToString() throw a null reference exception, and Convert.ToString(obj) will return an empty string.

So here are my recommendations:

  • (string)obj works best if you want to throw exceptions for types that cannot be assigned to a string variable (which includes zero)
  • obj as string works best if you do not want to throw any exceptions, and also do not want string representations to be not string representations
  • obj.ToString() works best if you want to throw exceptions for zero
  • Convert.ToString(obj) works best if you don't want to throw any exceptions and want the string representations not to be string
+2
source

The as string check is an object that is a string. If it is not null, it returns.

Calling ToString() will actually call the ToString() method on the object.

+1
source

The first returns the class as a string if the class is a string or obtained from a string (returns null if failed).

The second calls the ToString () method for the class.

+1
source

In fact, the best way to write the above code is to do the following:

 if (Session["SessionTheme"] != null) { Page.Theme = Session["SessionTheme"].ToString(); } 

That way, you are pretty sure that it will not use a NullReferenceException.

0
source

All Articles