Csharp/CSharp Tutorial/Operator/is

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

"is" operator for value data type: int, long and float

class MainClass
{
  public static void Main()
  {
    int myInt = 0;
    bool compatible = myInt is int;
    System.Console.WriteLine("myInt is int = " + compatible);
    compatible = myInt is long;
    System.Console.WriteLine("myInt is long = " + compatible);
    compatible = myInt is float;
    System.Console.WriteLine("myInt is float = " + compatible);
  }
}
myInt is int = True
myInt is long = False
myInt is float = False

is operator in class hiearchy

using System; 
 
class A {} 
class B : A {} 
 
class MainClass { 
  public static void Main() { 
    A a = new A(); 
    B b = new B(); 
 
    if(a is A) 
       Console.WriteLine("a is an A"); 
    if(b is A)  
      Console.WriteLine("b is an A because it is derived from A"); 
    if(a is B)  
      Console.WriteLine("This won"t display -- a not derived from B"); 
    if(b is B) 
      Console.WriteLine("B is a B"); 
    if(a is object) 
      Console.WriteLine("a is an object"); 
  } 
}
a is an A
b is an A because it is derived from A
B is a B
a is an object

Test if someObject is, or is derived from, a TextReader using the is operator

using System;
using System.IO;
class MainClass
{
    public static void Main() 
    {
        Object someObject = new StringReader("This is a StringReader");
        
        if (someObject is TextReader) 
        {
            Console.WriteLine(
                "is: someObject is a TextReader or a derived class");
        }
    }
}
is: someObject is a TextReader or a derived class

Type operators: Is

Testing a Type with "is".

You can determine whether an object is of a certain type by using the "is" operator.

Its general form is shown here:


expr is type

Use is in Console.WriteLine

using System;
public class MainClass
{
    static void Main() {
        String derivedObj = "Dummy";
        Object baseObj1 = new Object();
        Object baseObj2 = derivedObj;
        Console.WriteLine( "baseObj2 {0} String", baseObj2 is String ? "is" : "isnot" );
        Console.WriteLine( "baseObj1 {0} String", baseObj1 is String ? "is" : "isnot" );
        Console.WriteLine( "derivedObj {0} Object", derivedObj is Object ? "is" : "isnot" );
    }
}
baseObj2 is String
baseObj1 isnot String
derivedObj is Object

Use "is" to avoid an invalid cast.

using System; 
 
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; 
  } 
}

Working with Interfaces: is operator

using System;
interface Printable
{
    void MarginX(float factor);
    void MarginY(float factor);
}
public class Component
{
    public Component() {}
}
public class TextField: Component, Printable
{
    public TextField(string text)
    {
        this.text = text;
    }
    // implementing Printable.MarginX()
    public void MarginX(float factor)
    {
        Console.WriteLine("MarginX: {0} {1}", text, factor);
        // scale the object here.
    }
    
    // implementing Printable.MarginY()
    public void MarginY(float factor)
    {
        Console.WriteLine("MarginY: {0} {1}", text, factor);
        // scale the object here.
    }
    
    private string text;
}
class Test
{
    public static void Main()
    {
        Component[] dArray = new Component[100];
        
        dArray[0] = new Component();
        dArray[1] = new TextField("A");
        dArray[2] = new TextField("B");
        
        foreach (Component d in dArray)
        {
            if (d is Printable)
            {
                Printable scalable = (Printable) d;
                scalable.MarginX(0.1F);
                scalable.MarginY(10.0F);
            }
        }
    }
}
MarginX: A 0.1
MarginY: A 10
MarginX: B 0.1
MarginY: B 10