SYSK 94: as, is, where keywords in C#

There are a few keywords one should be aware of:

 

The as operator is similar to a cast but on any conversion failure null is returned as oppose to raising an exception.  Note: it will not do the conversion (cast != data type conversion)! 

 

Personally, I prefer not to use this operator, and, instead, use:

      if (x is MyType)

            // do the logic

      else

           // log error

 

As you can see, the is operator, which existed for some time, is used to check whether the run-time type of an object is compatible with a given type.

 

However, there are cases where as operator is useful.  For example, I've personally used it in helper classes as described by code snippet below:

    public class CompareHelper<T>

    {

        public int Compare(object data1, object data2)   // similar to string.compare

        {

            int result = 0;  

            if (typeof(T) == typeof(bool))

                result = Compare(data1 as bool, data2 as bool);

            else if (typeof(T) == typeof(string))

                result = Compare(data1 as string, data2 as string);

           else if (typeof(T) == typeof(SomeCustomClass))   // perhaps this calls for some property call

                result = Compare(data1 == null ? 0m : (data1 as SomeCustomClass).SomeDecimalProperty, data2 == null ? 0m : (data2 as SomeCustomClass).SomeDecimalProperty);

            . . .

        }

       

        public int Compare(bool data1, bool data 2)

        {

             . . .

        }

        public int Compare(decimal data1, decimal data 2)

        {

             . . .

        }

        . . .

    }

 

Finally, the new where keyword is used to specify constraints on the types that can be used as arguments for a type parameter defined in a generic declaration.  For example:

 

namespace Test

{

    public class Z

    {

        public virtual void Test()

        {

            System.Diagnostics.Debug.WriteLine("Z.Test");

        }

    }

    public class Y : X

    {

        public override void Test()

        {

            System.Diagnostics.Debug.WriteLine("Y.Test");

        }

    }

  public class X

    {

        public virtual void Test()

        {

            System.Diagnostics.Debug.WriteLine("X.Test");

        }

    }

    public class XX<T> where T : X

    {

        public virtual void Test()

        {

           System.Diagnostics.Debug.WriteLine("XX.Test");

        }

    }

    public partial class Form1 : Form

    {

            . . .

        private void button1_Click(object sender, EventArgs e)

        {

            XX<X> o1 = new XX<X>();

            o1.Test();

  XX<Y> o2 = new XX<Y>();

            o2.Test();

            // this line will fail at compile time with error:

            // The type 'Test.Z' must be convertible to 'Test.X' in order to use

            // it as parameter 'T' in the generic type or method 'Test.XX<T>'

            XX<Z> o3 = new XX<Z>();

            o3.Test();

        }

    }

}

 

So, go ahead, let the compiler do the work – use these operators to write solid code!