C# initialiser conditional assignment

后端 未结 6 1291
广开言路
广开言路 2021-01-04 01:49

In a c# initialiser, I want to not set a property if a condition is false.

Something like this:

ServerConnection serverConnection = new ServerConnect         


        
6条回答
  •  不知归路
    2021-01-04 02:23

    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)

提交回复
热议问题