#1
  1. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    C# equivalent for Java's instanceof keyword


    Hi,

    I am new to C#. I am working on a test program in C# to invoke web service for my test case. Is there any C# equivalent construct for Java's instanceof keyword?
    When the programming world turns decent, the real world will turn upside down.
  2. #2
  3. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    439
    "is" in C# == "instanceof" in Java.

    Or so I have heard (http://www.25hoursaday.com/CsharpVsJava.html). :)
    OMG RAVER CHICKS!!
    On a related note: C/C++ Programming Tutorials


    "Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
    "Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    instanceof in .NET


    Can use .NET is operator like above. However is requires an instance of the class you want to test. What if you just want to test the Type (class) itself? The following method does this.

    public static bool InheritsFrom(Type _type1, Type _type2)
    {
    Type currenttype = _type1;

    if (_type1.Equals(_type2)) { return true;}

    while (true)
    {
    currenttype = currenttype.BaseType;
    if (currenttype == null) { return false; }
    if (currenttype.Equals(_type2)) { return true; }

    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo