[WEBINAR] Better to Ask Permission? Best Practices for Privacy and Security

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#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
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.