
The C# language has many casting mechanisms. The as operator cast is often the most efficient and least prone to error. It readily handles casting your variable to a more derived type, but also fails without an exception if an incompatibility is found. For these reasons, the as cast is effective in most programs.
KeywordsTip: For reference types, the as-cast is recommended for its combination of performance and safety. You can test the casted variable against null and then use it, eliminating extra casts.

To start, this example shows a string variable being declared and then casted to the base object class type. Then, the object variable is casted to a string with the as operator. Next, the object is casted to a StringBuilder, but this cast fails because StringBuilder is not a more derived type of the original type of string.
Program that uses as casts [C#]
using System;
using System.Text;
class Program
{
static void Main()
{
// Create a string variable and cast it to an object.
string variable1 = "carrot";
object variable2 = variable1;
// Try to cast it to a string.
string variable3 = variable2 as string;
if (variable3 != null)
{
Console.WriteLine("have string variable");
}
// Try to cast it to a StringBuilder.
StringBuilder variable4 = variable2 as StringBuilder;
if (variable4 != null)
{
Console.WriteLine("have StringBuilder variable");
}
}
}
Output
have string variableSuccess:
When the as cast operator succeeds,
you will find that the variable to which you assign the result of the cast expression
can be used. In other words, the variable3 in the example is equal in function to
the original string.
Failure:
On the other hand, when you try an incompatible cast, such
as with the StringBuilder type, the result of the casting expression is null. You
can test the stored local variable variable4 against the null literal to test for
success.

The as cast construct in the C# language is sometimes the most efficient way to test for a certain type. This is because you can then utilize the variable after you have a successful cast. There is seldom any reason to use an is cast before an as cast. Excessive casting is a performance complaint in the FxCop static analysis tool. Also, the as cast avoids throwing an exception on an invalid cast, which in this case makes it much faster than an explicit cast.
FxCop Performance Warnings
At the instruction level in the intermediate language, you will see that the as cast is implemented in terms of the isinst opcode, the same used for the is cast. However, the isinst instruction is followed by instructions to store the local variable that was received by the ininst code, which results in the reduction of cast reduction.
Intermediate Language
We looked at the as cast operator in the C# language. The as cast receives two operands: the variable you want to cast, and the type you are trying to cast to, and then, upon evaluation, returns the casted variable or the null literal. The as cast is an efficient and fairly elegant way to cast in the C# language, promoting exception-neutral and less redundant code.
Cast Examples