Swacblooms🦋

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Uncategorized
C# bang bang operator
Uncategorized

C# bang bang operator

Sammy April 8, 2022

Hola 🥂🥂, so in the heat of releasing the DotNet 7 preview, developers have been raving about the parameter null-checking operator popularly called the bang-bang (!!) operator😜 in C#.

So let us talk about this feature 🥶

The parameter null-checking is one of the preview features for C# 11. Preview features are features that are still in consideration and might end up making it to becoming default features of the next major release.

Prior to the release of this feature, ArgumentNullException errors could be handled using the various format below:

void Method(string name) 
{
   if (name == null)
    {
        throw new ArgumentNullException(nameof(name));
    }
    // bla bla bla
}

But to try out the bang-bang operator in the c# 11 preview, you need to have at least Visual Studio 17.1 (Visual Studio 2022 Update 1) or .NET SDK 6.0.200.
At the time of writing this post, I am currently using the .NET SDK 6.0.201 to try out this feature.

You can download the SDK by clicking here

Steps To switch Into the preview mode and use the bang-bang operator:-

  • Open the “*.csproj” file >> and add a LangVersion element like the one below
<PropertyGroup>
   <LangVersion>preview</LangVersion>
</PropertyGroup>

  • Now you can use the code below:-
//Now you can you the !! operator
void Method(string name!!) 
{
    /* it would automatically throw the ArgumentNullException error if name is null */
}
// instead of this code 
void Method2(string name) 
{
   if (name == null)
    {
        throw new ArgumentNullException(nameof(name));
    }
    // bla bla bla
}

They have been a little bit of friction in terms of developers in the DotNet community accepting this feature.

Some advocated for the use of a notnull keyword which seems to be more understandable and intuitive at a glance.

To check out more about some of the preview features click this.

Thanks for reading 🥂

PS :- This feature later got removed you can look out for more details here.

Prev Article
Next Article

Related Articles

politenumbers
In this video I explained how to write an algorithm …

Polite numbers in C#

serving a vue js app from dotnet
Hey guys 👋👋, in this article, I will be describing …

Serving A VueJs App from DotNet

About The Author

Sammy

I am Samson Amaugo. I am a full-stack web developer and I specialize in MERN stack development

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫
  • Deploying a DotNet API To Railway 🚂
  • GitHub OAuth Device Flow in DotNet

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS Swacblooms

  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫
  • Deploying a DotNet API To Railway 🚂
  • GitHub OAuth Device Flow in DotNet
  • Resumable Download in DotNet
  • Mocking the HttpClient in C#
  • MySocials DotNet CLI Tool
  • Benchmarking in C#
  • C# bang bang operator

Swacblooms🦋

Making the Moves
Copyright © 2023 Swacblooms🦋
Swacblooms - Making the Moves