12 Shorthand Operators in C# Every Developer Should Know About

12 Shorthand Operators in C# Every Developer Should Know About

Last Updated on August 9, 2023 by Aram

In this article we will get introduced to 12 Shorthand Operators in C# Every Developer Should Know About, these operators are widely used in all projects.

Of course there are tons of other trivial and heavily used operators. Yet these ones are shorthand versions to write code in a clear and concise syntax.

C# is such a powerful language that includes such great operators.

In this article you will learn about the following shorthand operators in :

  • Conditional Operator ? : 
  • Null-Conditional Operator Member Access ?. 
  • Null-Conditional Operator Element Access ?[] 
  • Null-Coalescing operator ?? 
  • Null-Coalescing Assignment Operator ??= 
  • Null-Forgiving Operator ! 
  • Index Operator ^ 
  • Range Operator .. 
  • Expression Body Definition => 
  • Type-testing Operator is 
  • Type-testing Negation Operator is not
  • Type-casting operator as

So let’s see these 12 Shorthand Operators in C#:

Conditional Operator ? :

Or, the Ternary Operator, it is a shortened and a cleaner way to write if-else statement.

Null-Conditional Operator Member Access ?.

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 .. ^

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

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

Summary

These were some of the most commonly used shorthand operators in C# that every developer should know about.

Of course there are so many other operators and features that really important, but this article highlighted 12 operators with brief description and an example on each.

I hope this was a helpful article.

Learn about C#

One of the best ways to learn is to attend online courses, and what site is better than Udemy to find excellent courses to learn C# and become an expert in it.

I have gathered some recommended courses from Udemy below:

And guess what, you can now use this Udemy Coupon to get discount on Udemy courses.

Happy learning.

Learn about ASP.NET Core Web API

Here are some of my blog’s tutorials to help you learn ASP.NET Core Web API:

Advertisements and Sponsorships

This blog receives thousands of visitors each month.

Promote your product, company, or your work in front of Software engineers and technology specialists.

Contact me over aram@codingsonata.com for advertisements, sponsorships or other collaborations.

Bonus

Enjoy this brilliant masterpiece with its deep bass baroque tunes of strings

Leave a Reply