DEV Community

Cover image for A great feature in C# 9.0 (Simplified Parameter Null Validation)
Leonardo Gasparini Romão
Leonardo Gasparini Romão

Posted on • Edited on

4 4

A great feature in C# 9.0 (Simplified Parameter Null Validation)

Well, as C# advances, some features are been more simplified inside the language. One common problem that beginners programmers have is how to deal with nullable variables.

In the first versions of C# language, validate null values were very explicity when I was coding, eg...

public static void Main()
    {
        string Name = null;
        if(Name == null)
            throw new NullReferenceException
            ("Error, the name is null");
    }
Enter fullscreen mode Exit fullscreen mode

After version 4.0 of .Net Framework, C# has gain some other interesting options to use. eg...


public static void Main()
    {
        //Using ?? Operator
        string Name = null ?? "Undefined";
        Console.WriteLine(Name);

        Name = null;

        //Using String Extension Mehtods
        if(String.IsNullOrEmpty(Name))
            throw new NullReferenceException
            ("Error, the name is null or Empty");

    }

Enter fullscreen mode Exit fullscreen mode

But now with C# 9.0, I can simplify all of this with the !! Operator like:


public static void Main()
    {
        string Name = null;
        ManipulateString(Name);
        OtherManipulateString(Name)
    }

    //Using new ! Operator to throw exception
    public static string ManipulateString(string s!)
    {
        return s + " is a Valid string";
    }

    //Using new !! Operator to substitute null to other things like "" (probably)
    public static string OtherManipulateString(string s!!)
    {
        return s + " is a Valid string";
    }

Enter fullscreen mode Exit fullscreen mode

although it is too early to validate whether it will be interesting to use this operator, in fact, it seems an interesting option to consider, despite not making it so explicit in the code, the economy of lines of code may be surprising.

Useful links:
http://www.macoratti.net/20/02/c_versao9.htm
https://www.infoq.com/news/2020/06/CSharp-9-Null/
https://www.c-sharpcorner.com/article/null-value-and-null-reference-handling-c-sharp6-to-c-sharp-9-new-features-day-1/

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
shaijut profile image
Shaiju T • Edited

Nice 😄 , But I think its not readable. == null is better than s!

Collapse
 
lleonardogr profile image
Leonardo Gasparini Romão

Yeah, I think that == suits better for conditionals or for Object construction I guess, but to organize methods reducing the number of lines of code, plus the validation implicit makes Method(string s!) better than Method(string? s)...//string validation

But I'm certainly need test this more clearly 😄

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay