I’m learning C# and got a question regarding type casts.

Hi there! I'll get right to it.

/* given I have two types: Dog and Corgi, and I want to cast a ref. var. of type Dog to type Corgi. */ var dog = new Dog(); // example 1: var corgi = (Corgi) dog; // example 2: var corgi = dog as Corgi; 

What's the difference between these two ways of type casting? Are there situations where one is preferred over the other?

by fanfarius via /r/csharp

Leave a Reply