Find whether the class inherited from a particular interface

/* interface and ‘is’ operator*/

Definition:
By using ‘is’ operator we can find that, whether the class is inherited from a particular interface/class.

EXAMPLE 1:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace LearningConcepts
{

interface INode
{

string Text
{
get;
set;
}

object Tag
{
get;
set;
}

int Height
{
get;
set;
}

int Width
{
get;
set;
}

float CalculateArea();

}

public class Node : INode
{

public Node()
{ }
public string Text
{
get {return m_text;}
set {m_text = value;}
}

private string m_text;
public object Tag
{

get{return m_tag;}
set{m_tag = value;}

}

private object m_tag = null;
public int Height
{

get {return m_height;}
set {m_height = value;}

}

private int m_height = 0;
public int Width
{
Get {return m_width;}
Set {m_width = value;}

}

private int m_width = 0;
public float CalculateArea()
{

if ((m_width < 0) || (m_height < 0))
return 0;

return m_height * m_width;

}

}
class ClonableNode :ICloneable
{
public object Clone()
{
return null;
}

// INode members
}

class Program
{
static void Main(string[] args)
{
Node nodeC=new Node();
if (nodeC is INode)
Console.WriteLine(“nodeC is object of INode type”);
//this will be displayed
else
Console.WriteLine(“nodeC isn’t object of INode type”);
}
}
}

Advertisements

One response

  1. Thanks. It was useful.:)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: