Sunday, September 1, 2019

IS vs AS Operators in C#

is Operator:

The ‘is’ operator in C# is used to check the object type and it returns a bool value: true if the object is the same type and false if not.

For null objects, it returns false.

In other words, The “is” operator is used to check whether the run-time type of an object is compatible with a given type or not.



as Operator:

The ‘as‘ operator does the same job of ‘is‘ operator but the difference is instead of bool,
it returns the object if they are compatible to that type, else it returns null. In otherwords,
The ‘as‘ operator is used to perform conversions between compatible types.

No comments:

Post a Comment