Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Casting Performance Implications

Tags:

When using the 'as' keyword in C# to make a cast which fails, null gets returned. What's going on in the background? Is it simply suppressing an exception so I don't have to write handling code for a failure?

I'm interested in the performance characteristics of it compared to a typical cast wrapped in a try-catch.

like image 904
Daniel Revell Avatar asked Dec 11 '08 22:12

Daniel Revell


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

It's using the IL instruction isinst to perform the cast instead of the castclass instruction that is used when casting. This is a special instruction which performs the cast if it is valid, else leaves null on the stack if it isn't. So no, it doesn't just suppress an exception, and is orders of magnitude faster than doing so.

Note that there are some differences in behaviour between the isinst instruction and castclass - the main one being that isinst does not take into account user-defined cast operators, it only considers direct inheritance hierarchy, e.g. if you define the following two classes with no inheritance hierarchy but an explicit cast operator:

class A
{
    public int Foo;
}

class B
{
    public int Foo;

    public static explicit operator B(A a)
    {
        return new B { Foo = a.Foo };
    }
}

Then the following will succeed:

var a = new A { Foo = 3 };
var b = (B)a;
Console.WriteLine(b.Foo); // prints 3

However the following does not compile, with the error 'Cannot convert type 'A' to 'B' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion'

var a = new A { Foo = 3 };
var b = a as B;

So if you do have any user-defined casts set up (which are typically a bad idea on reference types, for this reason and others) then you should be aware of this difference.

like image 183
Greg Beech Avatar answered Oct 16 '22 10:10

Greg Beech