Difference between Convert.ToString() and .ToString()
Convert.ToString()
handles null
, while ToString()
doesn't.
Difference Between ToString() and Casting to String
it's not working because ID
has a different Type. It's not string
- so you can convert it but not cast it.
Difference between Convert.ToString Method and Object.ToString() with regard to Globalization
Yes and no.
For example, the Convert.ToString(int)
method, is identical to the Int32.ToString()
method, as MSDN states in the 'Remarks' section: "This implementation is identical to Int32.ToString()." However, the Convert
class also offers overrides which take an IFormatProvider
as a second parameter (e.g. Convert.ToString(int, IFormatProvider)
), and this can be used to adjust the output format, e.g. by passing a CultureInfo
instance.
Difference between Convert.ToString(Nullable int ) and Nullable int .ToString()?
Convert.ToString Method (Object, IFormatProvider):
If the value parameter implements the IConvertible interface, the
method calls the IConvertible.ToString(IFormatProvider) implementation
of value. Otherwise, if the value parameter implements the
IFormattable interface, the method calls its
IFormattable.ToString(String, IFormatProvider) implementation. If
value implements neither interface, the method calls the value
parameter's ToString() method.
Nullable<int>
is seen like standard int
, and IFormattable.ToString(String, IFormatProvider)
is fired when Convert.ToString
with format provider is called.
Proof:
class MyFormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
return "G";
}
}
static void Main(string[] args)
{
Nullable<int> SomeProperty = 1000000;
Console.WriteLine(SomeProperty.ToString());
Console.WriteLine(Convert.ToString(SomeProperty));
Console.WriteLine(Convert.ToString(SomeProperty, new MyFormatProvider()));
}
Put breakpoint within GetFormat
and it's going to be hit when the last one of Main
is executed.
Difference between .ToString and as string in C#
If Session["SessionTheme"]
is not a string
, as string
will return null
.
.ToString()
will try to convert any other type to string by calling the object's ToString()
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.
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 simply return null
.
Difference between converting integer to string +1 and 1.ToString()
The first method is equivalent to string str = "" + 1.ToString();
and uses 2 intermediate strings before producing the result. That amounts to 3 strings total: an empty string, "1", and the result of the concatenation, which is also "1".
The second method doesn't use any intermediate string. It's also more readable and clearly expresses your intent (which is to convert the integer into a string).
Related Topics
Is Task.Run Considered Bad Practice in an Asp .Net MVC Web Application
String Concatenation Using '+' Operator
Has Foreach's Use of Variables Been Changed in C# 5
How to Get the Propertyinfo of a Specific Property
How to Implement Ienumerable<T>
How to Correctly Cast a Class to an Abstract Class When Using Type Generics
Frombody String Parameter Is Giving Null
Using a Self-Signed Certificate with .Net's Httpwebrequest/Response
Token Based Authentication in Web API Without Any User Interface
Server Client Send/Receive Simple Text
Return Value from SQL Server Insert Command Using C#
Why Some Types Do Not Have Literal Modifiers