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:
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.
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);
}
}
1234
C# Operators |