C# FAQ: What is the CSharp equivalent of the Jplusplus instanceof operator?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

What is the C# equivalent of the J++ instanceof operator?

In C#, use the is operator, for example:

using System;
 
class ClassA {}
 
public class TestIs
{
   public static void Test (object o) 
   {
      ClassA a = null;
 
      if (o is ClassA) 
      {
         Console.WriteLine ("o is ClassA");
         a = (ClassA) o;
       }
      else 
      {
         Console.WriteLine ("o is not ClassA.");
      }
   }
 
   public static void Main() 
   {
      ClassA cA = new Class1();
      Test (cA);
      Test ("example string");
   }
}


Personal tools