Is And As Operators Explained With An UML

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.   
  5. public interface ISmartPhone  
  6. {  
  7.    string DisplayObject();  

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 { getset; }  
  4.        public double Price { getset; }  
  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.     
      8.            Phone notASmartPhone = new Phone();    
      9.            VerifyTheSmartPhone(notASmartPhone);    
      10.     
      11.            ISmartPhone interfaceReference = new SmartPhone() { Name = "One Plus 8", Price = 65000 };    
      12.            VerifyTheSmartPhone(interfaceReference);    
      13.        }    
      14.     
      15.        /// <summary>    
      16.        /// This methods checks if parameter is an smart phone type    
      17.        /// </summary>    
      18.        /// <param name="obj"></param>    
      19.        /// <returns>true if parameter is a smart phone</returns>    
      20.        static bool VerifyTheSmartPhone(object obj)    
      21.        {    
      22.           if(obj is SmartPhone)    
      23.           {    
      24.                SmartPhone smartPhone = (SmartPhone)obj;    
      25.                System.Console.WriteLine(smartPhone.DisplayObject());    
      26.                return true;    
      27.            }    
      28.            else    
      29.            {    
      30.                System.Console.WriteLine("It is not a SmartPhone object");    
      31.                return false;    
      32.            }    
      33.        }    
      34.    }    
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.