Both of these two operators -- is and as - execute at run time.

The is operator

The is operator is used for checking whether the object is compatible or not.
  • It returns true if the given object is of the same type, else it returns false.
  • It also returns false if the object is null.
  • Syntax - The following code checks if object is a SmartPhone or not.
    1. if(obj is SmartPhone)

The as operator

  • This operator is used for conversion between compatible reference types.
  • It returns the object if they are compatible, else it returns null.
  • Syntax - The following code coverts obj into SmartPhone. It's called typecasting.
    1. SmartPhone smartPhone = obj as SmartPhone;
  • Even this is allowed,
    1. SmartPhone smartPhone = (SmartPhone)obj;
Let's create a basic UML, then we will implement classes as per the UML, to understand these 2 operators.
  • Base class - Phone
  • Base Interface - ISmartPhone
  • Derived class - SmartPhone implementing an interface ISmartPhone & extending a class Phone

    • As per this implementation, we know that every SmartPhone is a Phone as well as ISmartPhone
    • But not every phone is a SmartPhone. As in the real world, some phones are just meant for calling & texting.
Is and As operators explained with an UML
Let's start coding.
First, Base classes and interfaces:
  1. public class Phone
  2. {
  3. }
  4. public interface ISmartPhone
  5. {
  6. string DisplayObject();
  7. }
Now the real deal, SmartPhone class,
  • It has properties such as Name & Price of a smartphone
  • Overridden method DisplayObject of ISmartPhone interface
  1. public class SmartPhone : Phone, ISmartPhone
  2. {
  3. public string Name { get; set; }
  4. public double Price { get; set; }
  5. public string DisplayObject()
  6. {
  7. return "Name: " + Name + "||"
  8. + " Price: " + Price;
  9. }
  10. }
Lastly, we need to call this structure through the Main method,
  • VerifyTheSmartPhone(): It takes an object as a parameter & checks if that object is a SmartPhone or not
  • If it is a SmartPhone then it calls the DisplayObject method of that SmartPhone
  • Else it will display a message
  • These operators help to handle the exception at runtime
  • Calling of VerifyTheSmartPhone(obj)

    • First, pass SmartPhone object
    • Second, pass Phone's object (Remember as per our UML, not all the Phones are SmartPhones)
    • Third, Another SmartPhone object.
      1. class Program
      2. {
      3. static void Main(string[] args)
      4. {
      5. object obj = new SmartPhone() { Name = "IPhone X", Price = 110000 };
      6. VerifyTheSmartPhone(obj);
      7. Phone notASmartPhone = new Phone();
      8. VerifyTheSmartPhone(notASmartPhone);
      9. ISmartPhone interfaceReference = new SmartPhone() { Name = "One Plus 8", Price = 65000 };
      10. VerifyTheSmartPhone(interfaceReference);
      11. }
      12. /// <summary>
      13. /// This methods checks if parameter is an smart phone type
      14. /// </summary>
      15. /// <param name="obj"></param>
      16. /// <returns>true if parameter is a smart phone</returns>
      17. static bool VerifyTheSmartPhone(object obj)
      18. {
      19. if(obj is SmartPhone)
      20. {
      21. SmartPhone smartPhone = (SmartPhone)obj;
      22. System.Console.WriteLine(smartPhone.DisplayObject());
      23. return true;
      24. }
      25. else
      26. {
      27. System.Console.WriteLine("It is not a SmartPhone object");
      28. return false;
      29. }
      30. }
      31. }
Let's see if this works.
Is and As operators explained with an UML
Hooray!! Both operators worked as expected.

Conclusion

In this article, we learned:
  • What the is & as operators are
  • How to use them
  • IS-A relationship
Thank you all, I hope you enjoyed the knowledge shared in this article.