Showing posts with label Nullable reference types. Show all posts
Showing posts with label Nullable reference types. Show all posts

Sunday, May 12, 2024

How to declare Nullable reference type in c#

 A variable of type Product?[] denotes an array that can contain Product or null values but that won’t be null itself:

Product?[] arr1 = new Product?[] { p1, p2, null }; // OK

Product?[] arr2 = null; // Not OK

A variable of type Product[]? is an array that can hold only Product values and not null values, but the array itself may be null:

Product[]? arr1 = new Product[]? { p1, p2, null }; // Not OK

Product[]? arr2 = null; // OK

A variable of type Product?[]? is an array that can contain Product or null values and that can itself be null:

Product?[]? arr1 = new Product?[] {p1, p2, null }; // OK

Product?[]? arr2 = null; // Also OK


C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...