简体   繁体   中英

Cannot implicitly convert from type 'void' to 'bool'

I hope that I am not asking a rather stupid question now. I have searched quite some time on Google, but I can't seem to find the answer -- or maybe I just lack the understanding of how it works.

I hope that someone can explain to me why this error occurs? I keep getting the error: "Cannot implicitly convert from type 'void' to 'bool'".

This is the code:

class User
{
    public string FirstName
    {
        get { return _FirstName; }
        set
        {
                 // the error occurs on the line below
            if((ArgChecker.ThrowOnStringNullOrWhiteSpace(_FirstName) ) )
            {
                Console.WriteLine("something1");
            }
            else { _FirstName = value; }
        }
    }
    private string _FirstName;
}


class ArgChecker
{
    public static void ThrowOnStringNullOrWhiteSpace(string arg)
    {
        if (string.IsNullOrWhiteSpace(arg))
        {
            throw new ArgumentNullException("something2");
        }
    }
}

You can not convert void to bool, Instead your property should be something like

public string FirstName
{
    get { return _FirstName; }
    set
    {
        ArgChecker.ThrowOnStringNullOrWhiteSpace(value);
        _FirstName = value; 
    }
}

您不应该将传递给 ThrowOnStringNullOrWhiteSpace 函数(而不是 _FirstName)吗?

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