Export (0) Print
Expand All

() Operator

In addition to being used to specify the order of operations in an expression, parentheses are used to specify casts (type conversions):

( type ) expr

Where:

type
The name of the type to which expr is to be converted.
expr
An expression.

Remarks

A cast explicitly invokes the conversion operator from expr's type to type; the cast will fail if no such conversion operator is defined. To define a conversion operator, see explicit and implicit.

Example

The following program casts a double to an int. The program won't compile without the cast.

// cs_operator_parentheses.cs
using System;
class Test 
{
   public static void Main() 
   {
      double x = 1234.7;
      int a;
      a = (int)x; // cast double to int
      Console.WriteLine(a);
   }
}

Output

1234

See Also

C# Operators | 7.6.6 Cast expressions | explicit

Show:
© 2015 Microsoft