C# initialiser conditional assignment

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

    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.

    0 讨论(0)
  • 2021-01-04 02:21

    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
    }
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-01-04 02:23

    How about this:

    ServerConnection serverConnection = new ServerConnection();  
    
    serverConnection.ServerInstance = server;  
    serverConnection.LoginSecure = windowsAuthentication;
    
    if (!windowsAuthentication)
    {
         serverConnection.Login = user;  
         serverConnection.Password = password;  
    }
    
    0 讨论(0)
  • 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,   
    }
    
    0 讨论(0)
  • 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
    };
    
    0 讨论(0)
提交回复
热议问题