Skip to content

Type checking and casting

Andrey Kurosh edited this page Feb 2, 2017 · 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

When using delegates in-place, their type is usually automatically inferred. However, it's also possible to cast delegate types as long as their signatures match exactly:

let fx = (x:int) -> x < 10 // Func<int, bool>
Array::FindAll
    <| new [1; 5; 10; 15; 20]
    <| fx as Predicate<int>

Shorthands

The following shorthands are supported in type definitions:

  • T? = Nullable<T>
  • T~ = IEnumerable<T>

Read more

Clone this wiki locally