简体   繁体   中英

Nullable getter, but non nullable setter

Using c# version 8.0 introduction of nullable reference types and non-nullable reference types, I can do something like this:

public class Response
{
    public Response(Exception? ex)
    {
        _ex = ex;
    }

    private Exception? _ex;

    public Exception? Ex
    {
        get => _ex;
        set { _ex = value; }
    }
}

I can initialize _ex to null via the constructor. However, once the instance is created I would like the setter to be unable to set Ex value to null (by removing the question mark from the type)... something like the code below, which of course, does not compile.

public class Response
{
    public Response(Exception? ex)
    {
        _ex = ex;
    }

    private Exception? _ex;

    public Exception? Ex
    {
        get => _ex;
    }

    public Exception Ex
    {
        set { _ex = value; }
    }
}

One work around of course is to set the _ex value with a method, instead of a setter, but I figured to ask anyway.

try this

public class Response
{
    public Response(Exception? ex)
    {
        _ex = ex;
    }

    private Exception? _ex;

    public Exception? Ex
    {
        get => _ex;
        set { 
            if ( _ex!=null && value==null ) throw exeption; // or just ignore maybe
           else  _ex = value; 
            }
    }
}

if Exeption is a reference type , and C# version allows reference types be null too, this is a right code also

public class Response
{
    public Response(Exception ex)
    {
        _ex = ex;
    }

    private Exception _ex;

    public Exception Ex
    {
        get => _ex;
        set { 
            if ( _ex!=null && value==null ) throw exeption; // or just ignore maybe
           else  _ex = value; 
            }
    }
}

the most important part of this code doesn't allow to set null if it is not null already. @golakwer code doesn't have this part:

if ( _ex!=null && value==null ) throw exeption; // or just ignore maybe
           else  _ex = value; 

That is the purpose of the [DisallowNull] attribute, which "specifies that null is disallowed as an input even if the corresponding type allows it".

Leave the property nullable so that any callers accessing the getter will be properly informed that its value may be null, but attempts to explicitly set it to null will generate the expected warning.

The attribute is added to the property, not the setter:

[DisallowNull]
public Exception? Ex
{
    get => _ex;
    set => _ex = value;
}

More info: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.disallownullattribute

(Also note that you can use => expression syntax for the set as well.)

If you add an ! after a variable that is nullable the compiler allows you to assign a non-nullable variable to a nullable variable.

So that Ex property can be non-nullable but in the constructor a nullable value can be assigned.

public class Response
{
    public Response(Exception? ex)
    {
        _ex = ex!;
    }

    private Exception _ex;

    public Exception Ex
    {
        get => _ex;
        set { _ex = value; }
    }
}

Further reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM