What is more effecient to use when converting data types in C#.
Convert.ToXXXX(object) method or (XXXXX)object way?
Printable View
What is more effecient to use when converting data types in C#.
Convert.ToXXXX(object) method or (XXXXX)object way?
i find the Convert.To method the best
eg:
in this example you can see another use of Convert , calculating time span differences :Code:string str="i'm some text " + Convert.ToChar(169);///169 is copyright symbol
MessageBox.Show(str);
Code:private void button8_Click(object sender, System.EventArgs e)
{
try
{
string strDate="11/06/2003 10:20:15";
string strFinish="11/06/2003 18:28:37";
TimeSpan s=Convert.ToDateTime(strFinish).Subtract(Convert.ToDateTime(strDate));
MessageBox.Show("Difference in time:\n" + s.ToString());
}
catch
{
System.Exception f = new System.Exception();
MessageBox.Show(f.Message);
}
}
for casting numbers to char i prefer a lot using (char)number as a char is just a kind of typedef of a ushort(from 0 to 65535 if i am not mistaken) so if u put a number from 0 to 65535(altough i think when using regular encoding you just use from 0 to 255) no error will happen and the code gets a lot easier to read from if u have many chars u want to convertQuote:
Originally posted by dynamic_sysop
i find the Convert.To method the best
eg:
in this example you can see another use of Convert , calculating time span differences :Code:string str="i'm some text " + Convert.ToChar(169);///169 is copyright symbol
MessageBox.Show(str);
Code:private void button8_Click(object sender, System.EventArgs e)
{
try
{
string strDate="11/06/2003 10:20:15";
string strFinish="11/06/2003 18:28:37";
TimeSpan s=Convert.ToDateTime(strFinish).Subtract(Convert.ToDateTime(strDate));
MessageBox.Show("Difference in time:\n" + s.ToString());
}
catch
{
System.Exception f = new System.Exception();
MessageBox.Show(f.Message);
}
}