Taoffi's blog

prisonniers du temps

Xamarin forms: elementary – don’t cast!

 

Many Xamarin users come from the Java environment and are starting with c#.

For those who don't know yet: please don't cast. Use the as operator instead.

Example:
In many situations, your method may seem like this:

void testcast(object img)
{
    Label    label1    = img as Label;    // label1 will simply be null
    Label    label2    = (Label)img;        // you get an InvalidCastException
}

 

Comments are closed