站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

8.14 Type Information Operators

The type information operators are used to get the type of the object at runtime. Because all objects inherit from the class System.Object, they all inherit the GetType method, which returns the type of the object. Java has one instanceof operator that you can use to check whether a particular object is an instance of a particular class. C# has operators that are the equivalent of Java's instanceof.

8.14.1 The is Operator

The is operator is used to check whether the runtime type of an object is compatible with a given type. The is operator is used in an expression of the following form:

expr is type

Here, expr is an expression of reference type. The is operator evaluates to true if both of the following conditions are met:

  • expr is not null.

  • expr can be cast to type. That is, a cast expression such as (type) expr does not throw an exception.

Listing 8.10 shows the use of the is operator.

Listing 8.10 Using the is Operator (C#)
using System;

class Test {

  public static void Main(string[] args) {
    object i = 3;

    if (i is Int32) {
      Console.WriteLine("It's an int");
    } else {
      Console.WriteLine("It's an "+i.GetType());
    }
  }
}

The output of Listing 8.10 is as follows:

It's an int

The is operator cannot be overloaded.

8.14.2 The as Operator

The as operator is used to perform conversions between compatible types. The as operator is used in an expression of the following form:

expr as type

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form

expr as type

is equivalent to

expr is type ? (type)expr : (type)null

except that expr is evaluated only once.

Listing 8.11 shows the use of the as operator.

Listing 8.11 Using the as Operator (C#)
using System;

using System.Collections;

class Test {

  public static void Main(string[] args) {

    object s = "Test";
    string t = s as string;
      ArrayList l = s as ArrayList;
      Console.WriteLine(t);
    Console.WriteLine((l==null));
  }
}

The output of Listing 8.11 is as follows:

Test
True

Because s is not obviously an ArrayList, the second line prints True because the ArrayList reference is null. The as operator cannot be overloaded.

8.14.3 The sizeof Operator

The sizeof operator is used to obtain the size (in bytes) of a value type. The sizeof operator can be applied only to value types, not reference types. The sizeof operator can be used only in the unsafe mode. The sizeof operator cannot be overloaded. Listing 8.12 shows the use of the sizeof operator.

Listing 8.12 Using the sizeof Operator (C#)
using System;

class Test {

  unsafe public static void Main(string[] args) {
    Console.WriteLine(sizeof(int));
    Console.WriteLine(sizeof(long));
  }

}

Here is the output of Listing 8.12:

4
8

Note that you must compile Listing 8.12 using the /unsafe option of the compiler.

8.14.4 The typeof Operator

The typeof operator is used to obtain the System.Type object for a type. A typeof expression takes this form:

typeof (type)

The typeof operator cannot be overloaded. Chapter 7 discusses the typeof operator in detail.

    [ directory ] Previous Section Next Section