Compartir a través de


() Operator (C# Reference)

In addition to being used to specify the order of operations in an expression, parentheses are used to perform the following tasks:

  1. Specify casts, or type conversions.

    double x = 1234.7;
    int a;
    a = (int)x; // Cast double to int    
    
  2. Invoke methods or delegates.

    TestMethod();
    

Remarks

A cast explicitly invokes the conversion operator from one type to another; the cast fails if no such conversion operator is defined. To define a conversion operator, see explicit and implicit.

The () operator cannot be overloaded.

For more information, see Casting and Type Conversions (C# Programming Guide).

A cast expression could lead to ambiguous syntax. For example, the expression (x)–y could be either interpreted as a cast expression (a cast of –y to type x) or as an additive expression combined with a parenthesized expression, which computes the value x – y.

For more information about method invocation, see Methods (C# Programming Guide).

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Operators

Concepts

C# Programming Guide

Other Resources

C# Reference