none
C# 8 - non-nullable string feature RRS feed

  • Question

  • Till C# 7, string can accept null values, why the concept of non-nullable is introduced in C# 8. This sounds weird. It doesn't makes sense to use "string?". Some one please clarify.


    If the idea is to avoid just null references. As a C# developer, I've noticed most of the C# developers handle string checking for null always. So this feature doesn't seem to be really helpful, rather introduced a confusion. I've seen Null reference exceptions mostly when objects are referred without checking if it's null. 


    IMO, I may be missing the greatness of this new feature, but this really introduce complexity in understanding what string type is. string basically should accept a "null". Non-nullable string doesn't makes sense.

    Friday, March 8, 2019 7:28 PM

All replies

  • There have been several articles written on the topic, for example:

    https://msdn.microsoft.com/en-us/magazine/mt829270.aspx

    The summary is that the nullable types are still nullable. Declaring them as non-nullable is a hint to the compiler so that it warns you if you use them in such a way that they might be null when they are not expected to be null. The idea behind it is to decrease the number of errors caused by developers who forget to write one of these "string checking for null" which you mentioned that they "always" do. Unfortunately, from time to time they miss one of these checks; the idea is that the compiler will be more intelligent in pointing out these errors if you give it more hints about how the variables are expected to be used.

    Friday, March 8, 2019 8:02 PM
    Moderator
  • Hi  Kathiresan

    Thank you for posting here.

    According to your description, you want to know more about non-nullable string feature in c#8.0;

    The following link has some specific code examples, you could have a look at it.

    https://devblogs.microsoft.com/dotnet/take-c-8-0-for-a-spin/

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 11, 2019 5:41 AM
    Moderator