C# initialiser conditional assignment

后端 未结 6 1256
广开言路
广开言路 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:45

    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
    };
    

提交回复
热议问题