Skip to content

Type checking and casting

Andrew edited this page Sep 5, 2013 · 7 revisions

Checking for a type

The is operator is used to check if an object is of a given type:

var a = 1
a is int // true
a is float // false

Casting to a type

An object can be casted to another type using the as operator:

var one = 1 as object // boxing

var obj = getObject ()
var casted = obj as SomeClass

Type casting plays an important role in type inference system. For example, it lets you initialize a variable with a null value:

var b = null as string

Clone this wiki locally