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.
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.
- SmartPhone smartPhone = obj as SmartPhone;
- Even this is allowed,
- 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.
Let's start coding.
First, Base classes and interfaces:
- public class Phone
- {
- }
-
- public interface ISmartPhone
- {
- string DisplayObject();
- }
Now the real deal, SmartPhone class,
- It has properties such as Name & Price of a smartphone
- Overridden method DisplayObject of ISmartPhone interface
- public class SmartPhone : Phone, ISmartPhone
- {
- public string Name { get; set; }
- public double Price { get; set; }
- public string DisplayObject()
- {
- return "Name: " + Name + "||"
- + " Price: " + Price;
- }
- }
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.
- class Program
- {
- static void Main(string[] args)
- {
- object obj = new SmartPhone() { Name = "IPhone X", Price = 110000 };
- VerifyTheSmartPhone(obj);
-
- Phone notASmartPhone = new Phone();
- VerifyTheSmartPhone(notASmartPhone);
-
- ISmartPhone interfaceReference = new SmartPhone() { Name = "One Plus 8", Price = 65000 };
- VerifyTheSmartPhone(interfaceReference);
- }
-
-
-
-
-
-
- static bool VerifyTheSmartPhone(object obj)
- {
- if(obj is SmartPhone)
- {
- SmartPhone smartPhone = (SmartPhone)obj;
- System.Console.WriteLine(smartPhone.DisplayObject());
- return true;
- }
- else
- {
- System.Console.WriteLine("It is not a SmartPhone object");
- return false;
- }
- }
- }
Let's see if this works.
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.