Share via

C# what is the meaning of default!

T.Zacks 3,996 Reputation points
Sep 17, 2022, 5:49 PM

See sample code
public record Person
{
public string FirstName { get; set; } = default!;
public string LastName { get; set; } = default!;
};

tell me what will be assign to property FirstName because i do not know what is the meaning of default!;
please guide me. thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,038 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. satya karki 986 Reputation points MVP
    Sep 18, 2022, 1:30 AM

    Hi @T.Zacks

    As per this article
    Default values
    The following categories of variables are automatically initialized to their default values:

    Static variables.
    Instance variables of class instances.
    Array elements.
    The default value of a variable depends on the type of the variable and is determined as follows:

    For a variable of a value_type, the default value is the same as the value computed by the value_type’s default constructor (§8.3.3).
    For a variable of a reference_type, the default value is null.
    Note: Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference.

    However, if you don't provide default value then you will get warning.
    Check this article for more insights.
    https://rijsat.com/2021/12/31/resolving-warning-non-nullable-property-must-contain-a-non-null-value-in-net-6/

    1 person found this answer helpful.

  2. Castorix31 86,036 Reputation points
    Sep 17, 2022, 5:59 PM

  3. Karen Payne MVP 35,441 Reputation points
    Sep 17, 2022, 7:37 PM

    A good way to look at the affect of a ! null forgiving operator is to tinker with Nullable setting in the project file or at the top of a class.

    242252-f1.png


  4. Rijwan Ansari 751 Reputation points MVP
    Sep 18, 2022, 1:39 AM

    Hi,

    Prior to .NET 6, null-state analysis and variable annotations are disabled for existing project, however, it is enabled for all .NET 6 projects by default. Because of which, all the properties or models are showing warning for non-nullable elements.

    Recent version of the Visual Studio 2022 and .NET 6, we have a common warning for the models or properties.
    Non-nullable property ‘propertyname’ must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

    After carefully observing this error message, it makes sense for those properties. In order to minimize the likelihood that, our code causes the runtime to throw System.NullReferenceException, we need to resolve this warning.

    Therefore, the compiler is giving warning in the solution that the default assignment of your properties (which is null) doesn’t match its state type (which is non-null string).

    To overcome this System.NullReferenceException and resolve this warning, we can assign a default value to the variable. Alternatively, we can check against null before doing any modification or operation.

    So, by adding default, we are initializing with default values.
    However, you can make it nullable by add ? in properties like as shown

    public class AppSetting {  
        public string? ReferenceKey { get; set; }  
        public string? Value { get; set; }   
        public string? Description { get; set; }  
    }  
    

    Or you can define with empty values like

    public class AppSetting {  
        public string ReferenceKey { get; set; } = string.empty;  
        public string Value { get; set; } = string.empty;  
        public string Description { get; set; } = string.empty;  
    }  
    

    Even you can disable this setting from .csproj file

    <Nullable>enable</Nullable>  
    

  5. Bruce (SqlWork.com) 67,016 Reputation points
    Sep 19, 2022, 9:26 PM

    default is compiler sugar to initialize an object to its default value (determined by type). the default for objects (vs value types) is null;

    if you have null checking enabled, all non-nullable variables must be set to a non-null value or a warning is generated, the ! suffix is used to disable this warning.

    string s1 = null; //compile ok with warning
    string s2 = default // compile ok with warning
    string s3 = default! // compiles ok with no warning.

    int len1 = s2.Length. // compile ok with waning because compiler knows s2 may be null - runtime error
    int len2 = s2!.Length // compiles ok with no warning, because we told the compiler s2 will not be null - runtime error
    int len3 = s3.Length // compiles ok with no warning - runtime error

    so in your code:

    public string FirstName { get; set; } = default!;

    is setting FirstName to null, but do not print warning. you will get a runtime error if FirstName is accessed before it is set to a value.

    you could used the more clear:

    public string FirstName { get; set; } = null!;

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.