Use parentheses, (), to call a method or invoke a delegate. is also known as the Elvis operator. Use the ?. For information about pointer element access, see the Pointer element access operator [] section of the Pointer related operators article.
The following example demonstrates how to access array elements: If an array index is outside the bounds of the corresponding dimension of an array, an IndexOutOfRangeException is thrown. You can omit any of the operands of the .. operator to obtain an open-ended range: For more information, see Indices and ranges. You also use square brackets to specify attributes: Available in C# 6 and later, a null-conditional operator applies a member access, ?., or element access, ? You can also use the ^ operator with the range operator to create a range of indices. If a evaluates to non-null, the result of a?.x or a? If you need an expression of type T, apply the null-coalescing operator ?? You can also use . The left-hand operand is an inclusive start of a range. The following example demonstrates how to call a method, with or without arguments, and invoke a delegate: You also use parentheses when you invoke a constructor with the new operator. The ?. Unlike array indices, which must be integer, the indexer parameters can be declared to be of any type. That is, if one operation in a chain of conditional member or element access operations returns null, the rest of the chain doesn't execute. If a evaluates to null, the result of a?.x or a? token to access a member of a namespace or a type, as the following examples demonstrate: Use a using directive to make the use of qualified names optional. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. to access an extension method. For example, if a is a non-null array instance and x is outside the bounds of a, a? As the preceding example shows, you also use square brackets when you declare an array type or instantiate an array instance. [], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null. For example, ^1 points to the last element of a sequence and ^length points to the first element of a sequence. The null-conditional member access operator ?. For more information about indexers, see Indexers. The null-conditional operators are short-circuiting. operator to check if a delegate is non-null and invoke it in a thread-safe way (for example, when you raise an event), as the following code shows: That code is equivalent to the following code that you would use in C# 5 or earlier: That is a thread-safe way to ensure that only a non-null handler is invoked. The following examples demonstrate the usage of the ?. [x] would throw the same exception for non-null a. You also use parentheses to adjust the order in which to evaluate operations in an expression. The right-hand operand is an exclusive end of a range. In the following example, B is not evaluated if A evaluates to null and C is not evaluated if A or B evaluates to null: If A might be null but B and C would not be null if A is not null, you only need to apply the null-conditional operator to A: In the preceding example, B is not evaluated and C() is not called if A is null. For more information, see C# operators.
However, if the chained member access is interrupted, for example by parentheses as in (A?.B).C(), short-circuiting doesn't happen. That is. The following example uses the .NET Dictionary
to a null-conditional expression, as the following example shows: In the preceding example, if you don't use the ?? [] operators: The first of the preceding two examples also uses the null-coalescing operator ??
operator, numbers?.Length < 2 evaluates to false when numbers is null. As the preceding example shows, expression ^e is of the System.Index type. Use conditional delegate call (style rule IDE1005). Use indexers to support indexing with user-defined types. If a.x or a[x] throws an exception, a?.x or a? Available in C# 8.0 and later, the .. operator specifies the start and end of a range of indices as its operands. In expression ^e, the result of e must be implicitly convertible to int. Because delegate instances are immutable, no thread can change the object referenced by the handler local variable. [x] would throw an IndexOutOfRangeException. [x] is of the corresponding nullable value type T?. Square brackets, [], are typically used for array, indexer, or pointer element access. For more information about arrays, see Arrays. For more information, see the following sections of the C# language specification: For more information about indices and ranges, see the feature proposal note. For more information, see Indices and ranges. The ., (), ^, and .. operators cannot be overloaded. [x] is null. In particular, if the code executed by another thread unsubscribes from the PropertyChanged event and PropertyChanged becomes null before handler is invoked, the object referenced by handler remains unaffected.
Cast expressions, which perform explicit type conversions, also use parentheses. [x] is the same as the result of a.x or a[x], respectively. For a sequence of length length, ^n points to the element with offset length - n from the start of a sequence. and ? In expression a..b, the results of a and b must be implicitly convertible to int or Index. Available in C# 8.0 and later, the ^ operator indicates the element position from the end of a sequence. Either of operands can be an index from the start or from the end of a sequence, as the following example shows: As the preceding example shows, expression a..b is of the System.Range type.