C#初始化程序条件赋值 [英] C# initialiser conditional assignment

查看:59
本文介绍了C#初始化程序条件赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#初始化程序中,如果条件为假,我不想设置属性.

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

类似这样的东西:

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

可以做到吗?怎么样?

推荐答案

您不能执行此操作;C#初始化程序是名称=值"对的列表.有关详细信息,请参见此处: http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5

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

您需要将 if 块移至下一行.

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

这篇关于C#初始化程序条件赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆