C# initialiser conditional assignment

孤者浪人 提交于 2019-11-30 17:34:01

You can't do this; C# initializers are a list of name = value pairs. See here for details: http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5

You'll need to move the if block to the following line.

This is not possible in an initializer; you need to make a separate if statement.

Alternatively, you may be able to write

ServerConnection serverConnection = new ServerConnection()  
{  
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,  
    Password = windowsAuthentication ? null : password
};

(Depending on how your ServerConnection class works)

I suspect this would work, but using logic this way sort of defeats the purpose of using the initializer.

ServerConnection serverConnection = new ServerConnection()  
{  
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,
    Password = windowsAuthentication ? null :password
};

Note: I do not recommend this approach, but if it must be done in an initializer (i.e. you're using LINQ or some other place where it has to be a single statement), you can use this:

ServerConnection serverConnection = new ServerConnection()
{
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,
    Password = windowsAuthentication ? null : password,   
}

As others mentioned, this can't exactly be done within an initializer. Is it acceptable to just assign null to the property instead of not setting it at all? If so, you can use the approach that others have pointed out. Here's an alternative that accomplishes what you want and still uses the initializer syntax:

ServerConnection serverConnection;
if (!windowsAuthentication)
{
    serverConection = new ServerConnection()
    {
        ServerInstance = server,
        LoginSecure = windowsAuthentication,
        Login = user,
        Password = password
    };
}
else
{
    serverConection = new ServerConnection()
    {
        ServerInstance = server,
        LoginSecure = windowsAuthentication,
    };
}

In my opinion, it shouldn't really matter much. Unless you're dealing with anonymous types, the initializer syntax is just a nice to have feature that can make your code look more tidy in some cases. I would say, don't go out of your way to use it to initialize all of your properties if it sacrifices readability. There's nothing wrong with doing the following code instead:

ServerConnection serverConnection = new ServerConnection()  
{
    ServerInstance = server,
    LoginSecure = windowsAuthentication,
};

if (!windowsAuthentication)
{
    serverConnection.Login = user,
    serverConnection.Password = password
}

How about this:

ServerConnection serverConnection = new ServerConnection();  

serverConnection.ServerInstance = server;  
serverConnection.LoginSecure = windowsAuthentication;

if (!windowsAuthentication)
{
     serverConnection.Login = user;  
     serverConnection.Password = password;  
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!