A personal repository of technical notes. - CSC

C# Casting

() Operator (C# Reference)

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)

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)

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