0

I just ran across another way to cast objects in C#. I have always used (CastType)variable. I just noticed some code using variable as CastType. The latter reminds me of VB.

Is there a difference between the two methods of casting other than syntax?

JabberwockyDecompiler
  • 3,188
  • 2
  • 37
  • 52

1 Answers1

6

The first one will throw InvalidCastException if the types don't match (refer to the documentation - "Explicit conversions" section). The second one (the as operator) will produce null value instead.

BartoszKP
  • 33,416
  • 13
  • 100
  • 127