[WEBINAR] The New Normal: Dealing with the Reality of an Unsecure World

As Operator

Definition - What does As Operator mean?

As operator, in C#, is an operator that is used to convert a reference type expression from one type to another compatible type.

The as operator attempts to perform a silent cast of an object to a given type without raising any exception upon failure. It can be used to compare two types. Because it is not used for user-defined conversion, it is more consistent than a normal cast. It never creates temporary objects and does not require the target variable to be converted to null before conversion. It is usually used when the type of the expression or object to which it has to be cast is not already known.

The as operator provides an elegant method of performing cast operation by making the code more readable without any code for exception handling. This results in reduced code size and lesser development effort. For reference types, the as operator is a good alternative to the cast operator because it is safer and provides better performance.

Techopedia explains As Operator

The as operator is a type testing operator with two parameters that include the expression of reference type that needs to be converted and the destination type to which the conversion has to be performed. If the conversion is successful, it returns the converted object, or null on failure.

For example, the as operator can be used by a collection object that can store a list of items of different types - such as integer, string and objects of user-defined types - and iterate through the collection to access each of the items as a string type.

The as operator is used to perform reference and boxing conversions only.

Although the as operator performs similarly to the cast operator, it differs in that it is only applicable to conversions between reference types without the occurrence of exceptions after cast operation. It cannot be used for user-defined conversions.

While performing type conversions using the as operator, the return value has to be checked for null before using the variable that is converted. When used with the generic method, which can receive reference and value types as input, the method has to be restricted to only accept reference types.

The as operator is more efficient than the is operator because it not only tests for the success of a cast but also returns a cast value for a successful cast operation, or null on failure.

This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
"Techopedia" on Twitter

Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.