is operator

In this chapter you will learn:

  1. Get to know is operator
  2. How to use is operator with interface
  3. Use 'is' to avoid an invalid cast
  4. Runtime check with in keyword

Using is operator

is operator tells if a reference is a certain class. We can determine whether an object is of a certain type by using the "is" operator.

Its general form is shown here:

expr is type
using System;//  ja  va2  s  . c om
class Person
{
    public string name;
}
class Employee : Person
{
    public string companyName;
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Employee();

        Console.WriteLine(p is Employee);
    }
}

The output:

is interface

The following code shows how to use the is Keyword to Work with an Interface.

using System;/* j  a  v  a  2  s .  com*/
   
public interface IPrintMessage
{
    void Print();
};
   
class Class1
{
    public void Print()
    {
        Console.WriteLine("Hello from Class1!");
    }
}
   
class Class2 : IPrintMessage
{
    public void Print()
    {
        Console.WriteLine("Hello from Class2!");
    }
}
   
class MainClass
{
    public static void Main()
    {
        PrintClass   PrintObject = new PrintClass();
   
        PrintObject.PrintMessages();
    }
}
   
class PrintClass
{
    public void PrintMessages()
    {
        Class1      Object1 = new Class1();
        Class2      Object2 = new Class2();
   
        PrintMessageFromObject(Object1);
        PrintMessageFromObject(Object2);
    }
   
    private void PrintMessageFromObject(object obj)
    {
        if(obj is IPrintMessage)
        {
            IPrintMessage PrintMessage;
   
            PrintMessage = (IPrintMessage)obj;
            PrintMessage.Print();
        }
    }
}

is for invalid casting

using System; //from  j a va2s. c  o m
 
class A {} 
class B : A {} 
 
class CheckCast { 
  public static void Main() { 
    A a = new A(); 
    B b = new B(); 
 
    if(a is B)
      b = (B) a; 
  } 
}

Runtime check with in keyword

The following code show to choose between two overloaded methods at run-time using the 'is' keyword.

using System;//from   j  a  va 2  s. co m

public class BankAccount {
    virtual public void Withdraw() {
        Console.WriteLine("Call to BankAccount.Withdraw()");
    }
}

public class SavingsAccount : BankAccount {
    override public void Withdraw() {
        Console.WriteLine("Call to SavingsAccount.Withdraw()");
    }
}

public class MainClass {

    public static void Main(string[] strings) {
        BankAccount ba = new BankAccount();
        Test(ba);

        SavingsAccount sa = new SavingsAccount();
        Test(sa);
    }
    public static void Test(BankAccount baArgument) {
        if (baArgument is SavingsAccount) {
            SavingsAccount saArgument = (SavingsAccount)baArgument;
            saArgument.Withdraw();
        } else {
            baArgument.Withdraw();
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. What is null reference and how to use null
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances