【问题标题】:C# initialiser conditional assignmentC# 初始化器条件赋值
【发布时间】:2011-03-14 20:07:47
【问题描述】:

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

类似这样的:

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

可以吗? 怎么样?

【问题讨论】:

    标签: c# initializer


    【解决方案1】:

    这在初始化程序中是不可能的;您需要单独创建一个if 声明。

    或者,你也可以写

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

    (取决于您的 ServerConnection 课程的工作方式)

    【讨论】:

    • 那是我的错。在您添加三级运营商之前,当您的答案只是不可能时,我最初对您投了反对票。现在它显然已锁定,我无法撤销投票。感谢服务器 barfs!
    • @Randolpho:现在你可以撤销它了。
    • @Randolpho ...三元运算符,而不是三元运算符。
    • 扩展“根据您的ServerConnection 类的工作方式,如果您的构造函数分配您希望为Login 保留的默认属性值(null 除外),这将不起作用或Password.
    【解决方案2】:

    你不能这样做; C# 初始值设定项是名称 = 值对的列表。详情见这里:http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5

    您需要将if 块移动到以下行。

    【讨论】:

    • 有更好的方法,就像下面提到的@SLaks
    【解决方案3】:

    我怀疑这会起作用,但是以这种方式使用逻辑有点违背了使用初始化程序的目的。

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

    【讨论】:

      【解决方案4】:

      正如其他人提到的,这不能完全在初始化程序中完成。将 null 分配给属性而不是根本不设置它是否可以接受?如果是这样,您可以使用其他人指出的方法。这是完成您想要的并且仍然使用初始化语法的替代方法:

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

      在我看来,这并不重要。除非您正在处理匿名类型,否则初始化语法只是一个很好的功能,它可以使您的代码在某些情况下看起来更整洁。我想说,如果它牺牲了可读性,请不要使用它来初始化所有属性。改为执行以下代码并没有错:

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

      【讨论】:

        【解决方案5】:

        注意:我不推荐这种方法,但如果它必须在初始化程序中完成(即您使用 LINQ 或其他必须是单个语句的地方),您可以用这个:

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

        【讨论】:

          【解决方案6】:

          这个怎么样:

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

          【讨论】:

            猜你喜欢
            • 2011-01-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-17
            • 1970-01-01
            相关资源
            最近更新 更多