Please tell what is the difference between is
and as
keyword in C#
Please tell what is the difference between is
and as
keyword in C#
The is
operator checks if an object can be cast to a specific type.
Example:
if (someObject is StringBuilder) ...
The as
operator attempts to cast an object to a specific type, and returns null if it fails.
Example:
StringBuilder b = someObject as StringBuilder;
if (b != null) ...
Also related:
The cast operator attempts to cast an object to a specific type, and throws an exeption if it fails.
Example:
StringBuilder b = (StringBuilder)someObject.