Hi Guys
NP86 casting and IConvertible
What is the different between casting and IConvertible Interface. In my view both can convert types therefore performing same task. Anyone knows please explain.
Thank you
Hi Guys
NP86 casting and IConvertible
What is the different between casting and IConvertible Interface. In my view both can convert types therefore performing same task. Anyone knows please explain.
Thank you
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Apr 7, 2008, 2:39 PM
Thank you, Alan.
AlanPosted Apr 7, 2008, 12:28 PM
It doesn't actually matter what parameter you provide because it's simply ignored :)
The method always returns Convert.ToInt32(this). The only reason for the IFormatProvider parameter is to satisfy the IConvertible interface.
You'll see some more interesting results when numbers are converted to their string representations as this program shows:
using System;
using System.Globalization;
class Test
{
static void Main()
{
double d = 2000;
CultureInfo ci = new CultureInfo("fr-FR");
Console.WriteLine(d.ToString("N",ci));
}
}
/*
output is 2 000,00
If change culture to en-US
output is 2,000.00
*/
Posted Apr 7, 2008, 11:38 AM
In the above program if we used System.Globalization and CultureInfo.CurrentCulture instead of null. Program is producing similar result. Anyone knows please explain the reason.
using System;
using System.Globalization;
class Test
{
static voidMain ()
{
Int64 i64 = 2L;
Int32 i32 = ((IConvertible)i64).ToInt32(CultureInfo.CurrentCulture);
Console.WriteLine(i32); // output is 2
}
}
AlanPosted Apr 4, 2008, 5:56 AM
Maha,
Check out the MSDN examples for the overload of the Convert.ToInt32() method which takes an IFormatProvider argument to see what I'm talking about here:
http://msdn2.microsoft.com/en-us/library/d7e175yd.aspx
Posted Apr 3, 2008, 5:10 PM
“Where the Convert class is useful is where you're converting the string representation of numbers into the actual numbers they represent. This cannot be done by casting as (in general) it involves considerations such as culture and formatting which need to be specified as arguments to a method.”
It is much appreciated if you support above one with example.
AlanPosted Mar 16, 2008, 8:00 AM
All the basic .NET types, such as Int32, Int64, Double and String, implement the IConvertible interface. However, they implement it using 'explicit interface implementation' which means that the relevant member can only be accessed using an IConvertible reference. For example:
using System;
class Test
{
static void Main()
{
Int64 i64 = 2L;
Int32 i32 = ((IConvertible)i64).ToInt32(null);
Console.WriteLine(i32); // prints 2
}
}
This would clearly be too tedious to use in practice and instead .NET provides the Convert class which provides static methods which do the same thing:
Int32 i32 = Convert.ToInt32(i64); // much easier to write
However, the same thing can be achieved with a simple cast:
Int32 i32 = (Int32)i64; // easier still to write
As well as being the easiest to write, a cast should also be preferred on efficiency grounds. This is because the casting rules are built into the C# compiler and so can be translated directly to the appropriate 'conv' instruction in MSIL. Obvious errors can also be picked up at this stage.
If you use the Convert class instead, the compiler just links to the appropriate method which is then called by the runtime. So more work is needed to achieve the same result and there's more chance of an exception being thrown.
Where the Convert class is useful is where you're converting the string representation of numbers into the actual numbers they represent. This cannot be done by casting as (in general) it involves considerations such as culture and formatting which need to be specified as arguments to a method.
So, to sum up, I'd use casting if you can and conversion if you can't.