A personal repository of technical notes. - CSC

C# Casting

() Operator (C# Reference)
http://msdn.microsoft.com/en-us/library/0z4503sa.aspx

double x = 1234.7;
int a;
a = (int)x; // Cast double to int


Remarks: A cast explicitly invokes the conversion operator from one type to another; the cast fails if no such conversion operator is defined.


as (C# Reference)
http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

string s = someObject as string;
if (s != null)
{
// someObject is a string.
}


The as operator is similar to a cast operation; however, if the conversion is not possible, as returns null instead of raising an exception. More formally, an expression of the form,

expression as type


Convert.ToInt32 Method (System)
http://msdn.microsoft.com/en-us/library/system.convert.toint32(VS.80).aspx

Return Value: A 32-bit signed integer equivalent to the value of value, or zero if value is a null reference

No comments:

Post a Comment