Technically speaking, Int64 and Long are equivalent in .NET
But if you convert one value into Int64, and cast the same value in Long the result is different.
Lets take one example:
(Long)27.5 returns 27
Whereas, Convert.ToInt64(27.5) returns 28
(Int64)27.5 returns 27
This is because, in C# (as well as in Java, and C++), the integer cast operation truncates - and so (Long)27.5 returns 27, whereas Convert has its own logic, and it rounds rather than truncating and so it returns 28.
Showing posts with label cast. Show all posts
Showing posts with label cast. Show all posts
Wednesday, April 11, 2012
C#.NET - Difference between Convert.ToString(), .ToString(), and (string) cast
Convert.ToString(), .ToString() and (string)
All these are used for converting an object into a string, but they differ the way they handle NULL values.
>> Convert.ToString() returns String.Empty while attempting to convert a NULL object into a string.
>> .ToString() throws an exception in such cases
>> While, (string) cast sets a string variable as "null" value, and do not throw any exception, but if you try to access any property, method it throws an exception.
All these are used for converting an object into a string, but they differ the way they handle NULL values.
>> Convert.ToString() returns String.Empty while attempting to convert a NULL object into a string.
>> .ToString() throws an exception in such cases
>> While, (string) cast sets a string variable as "null" value, and do not throw any exception, but if you try to access any property, method it throws an exception.
Subscribe to:
Comments (Atom)