0% found this document useful (0 votes)
7 views20 pages

12 Shorthand Operators in C# Every Developer Should Know Ab

The document outlines 12 shorthand operators in C# that developers should be familiar with, including the Conditional Operator, Null-Conditional Operators, and various others introduced in recent C# versions. It explains the functionality and usage of each operator, highlighting their benefits for cleaner and safer code. The content is aimed at enhancing the understanding of C# operators for improved coding practices.

Uploaded by

akirakaneki00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

12 Shorthand Operators in C# Every Developer Should Know Ab

The document outlines 12 shorthand operators in C# that developers should be familiar with, including the Conditional Operator, Null-Conditional Operators, and various others introduced in recent C# versions. It explains the functionality and usage of each operator, highlighting their benefits for cleaner and safer code. The content is aimed at enhancing the understanding of C# operators for improved coding practices.

Uploaded by

akirakaneki00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

12 Shorthand

Operators
Every C# Developer
Should Know About

Aram Tchekrekjian @AramT87


Conditional Operator ? :

Or, the Ternary Operator, it is a shortened and a cleaner


way to write if-else statement.
Null-Conditional Operator
Member Access ?.
AKA. Null Propagator, added in C# 6, this is used to
allow safe access for object properties, so a null object
would just return a null instead of throwing an exception
Null-Conditional Operator
Element Access ?[]
This is similar to the previous operator, but used with
Collections and Arrays rather than objects
Null-Coalescing Operator ??
Existing since C# 2, it is used to combine the value of an
operand with null to generate one value, which is either
the value of the left-hand operand if it is not null, or it is
the value of the right-hand operand otherwise.
Chained Null-Coalescing Operator ?? ??
The order of execution is done from right to left, this is a
very clean way to return a default value if there are
multiple null reference types, on the same line.
Null-Coalescing & Null-Conditional
Operators ?. ??
A very useful combination to check for null object as
well the nullability of the accessed member with
returning a default value if any of which is null
Null-Coalescing Assignment
Operator ??=
Added in C# 8, it is similar to the Null-Coalescing
operator, but can be used to assign the value of the left-
operand with the value of the right-operand in case the
left-operand was null
Null-Forgiving Operator !
Or the Null-Suppression operator, introduced in C# 8.0
It is used to suppress a compile time null warning when
you are sure the object won't be null
Index Operator ^
Also called, index from end operator, introduced in C# 8,
it is used to access element of a collection from the end
of it rather than from the beginning
Range Operator ..

Another operator introduced in C# 8, the range operator


can be used to slice or take a range of values from any
part of an array.

It can be used in combination with the Index Operator.

Note that the collections like list do not support the


range operator, yet you can either write an extension
method to implement it,

Or you can use the List.GetRange method to achieve the


same result
Range and Index Operators in C#
.. ^
Introduced in C# 8 to provide flexible operators to work with collections,
both operators can be used separately or combined
Expression Body Type Definition =>

Introduced in C# 6, this is a shorthand way to define the


implementation of a function, property, constructor and
others, with a single expression
Type Testing Operator is

The is operator does a runtime type-check for


expressions, it supports implicit boxing/unboxing
without having to use cast.

From C#7, you can use the is operator with pattern


matching, like property, constant, var

And starting from C# 9, you can start using the is


operator in declaration, type, relational pattern matching

Slide next to see the sample code:


Type Testing Operator is
Type Testing Negation Operator
is not
Introduced in C# 9, this shorthand operator can be used
to negate a pattern matching, which is simply the
negation of the is operator
Type-casting Operator as
Converts a value to a reference type or nullable value
type. If the conversion isn't possible, a null value will be
returned.

Unlike when using a cast expression, if the conversion


isn't possible, an exception will be thrown at runtime

See the next slide for examples:


Type-casting Operator as
Found this Useful?

Consider Reposting
Thank You
Follow me for more content

Aram Tchekrekjian

AramT87

CodingSonata.com/newsletters

You might also like