The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

.NET/C#: check for exact type: simple things are always a good solution (via: Stack Overflow)

Posted by jpluimers on 2013/10/17

A while ago, I bumped into a situation where someone had tried to solve the problem below with a convoluted reflection based solution for this seemingly simple problem:

class A {}
class B : A {}
B b = new B();
if(b is A) // this should return false

And indeed the solution is simple: replace the “b is a” with this:

b.GetType() == typeof(A)

Thanks StackOverflow users ChaosPandion and John K.

–jeroen

via c# check for exact type – Stack Overflow.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.