【问题标题】:How to control output by if statementif语句如何控制输出
【发布时间】:2019-07-05 10:51:51
【问题描述】:
if (vm.Name != null)
{
    Console.WriteLine("VM name is \"{0}\" and ID is \"{1}\". State is: \"{2}\". Location: \"{3}\" and the Instance Type is \"{4}\". Key is \"{5}\".",
        vm.Name, vm.InstanceId, vm.State, vm.Region, vm.InstanceType, vm.KeyName);
}
else
{
    Console.WriteLine("VM ID is \"{0}\". State is: \"{1}\". Location: \"{2}\" and the Instance Type is \"{3}\". Key is \"{4}\".",
        vm.InstanceId, vm.State, vm.Region, vm.InstanceType, vm.KeyName);
}

我正在尝试尽可能少地复制粘贴。我的问题是如何缩小此代码,以便仅将 if-statement 应用于信息的第一位 vm.Name 而不是整个输出行?

【问题讨论】:

  • 我认为您可以在这里将第一句与第二句分开构建。
  • 你研究过字符串插值和条件逻辑吗? thebillwagner.com/Blog/Item/…
  • 句子重构。完全值得。

标签: c# .net if-statement console.writeline


【解决方案1】:

你可以使用类似的东西,

    //Here you will check condition and format first few words of sentences
    var str = vm.Name != null ? $"name is {vm.Name} and " : string.Empty;
    //if name is not null then add it to zeroth position otherwise add empty string
    Console.WriteLine($"VM {str}ID is {vm.InstanceId}. State is: {vm.State}. Location: {vm.Region} and the Instance Type is {vm.InstanceType}. Key is {vm.KeyName}.");

奖金:.net fiddle

【讨论】:

    【解决方案2】:

    使用表达式。像这样的:

    Console.WriteLine(
        "VM {0}ID is \"{1}\". State is: \"{2}\". Location: \"{3}\" and the Instance Type is \"{4}\". Key is \"{5}\".",
        vm.Name != null ? $"name is \"{vm.Name}\" and " : string.Empty, vm.InstanceId,
        vm.State, vm.Region, vm.InstanceType, vm.KeyName);
    

    【讨论】:

    • 我喜欢这个解决方案 - 简洁明了。谢谢。
    【解决方案3】:

    你知道它总是有的部件并更换它......?这不是最好的,但应该可以。

    var output = string.Format("VM ID is \"{0}\". State is: \"{1}\". Location: \"{2}\" and the Instance Type is \"{3}\". Key is \"{4}\".",
                 vm.InstanceId, vm.State, vm.Region, vm.InstanceType, vm.KeyName);
    
    if (vm.Name != null) {
        output.Replace("VM ", $"VM name is "\"{vm.Name}\" ")
    }
    

    【讨论】:

      【解决方案4】:

      您可以将Name 属性声明为可为空

      然后做这样的事情:

      string val;
                  if (vm.Name != null)
                  {
                      val = "VM name is \"{0}\" and";
                  }
                  else
                  {
                      val = "VM";
                  }
                  Console.WriteLine(
                        val + " ID is \"{1}\". State is: \"{2}\". Location: \"{3}\" and the Instance Type is \"{4}\". Key is \"{5}\".",
                        vm.Name, vm.InstanceId,
                        vm.State, vm.Region, vm.InstanceType, vm.KeyName);
      

      【讨论】:

        【解决方案5】:
        var firstPart = string.Empty;
        if (vm.Name != null)
        {
            firstPart = $"VM name is {vm.Name}";
        }
        else
        {
            firstPart = $"VM ID is {vm.InstanceId}";            
        }
        
        Console.WriteLine($"{firstPart}. State is: {vm.State}. Location: {vm.Region} and the Instance Type is { vm.InstanceType}. Key is {vm.KeyName}.");
        

        您可以更改firstPart 变量的名称。我没有想到更好的事情。

        【讨论】:

          【解决方案6】:

          我会这样做。不错,简单易读。

          string VmName = "";
          
          if(vm.Name != null)
            VmName = "name is \"" + vm.Name + "\" and"; 
          
          Console.WriteLine("VM " + VmName + " ID is \"{0}\". State is: \"{1}\". Location: \"{2}\" and the Instance Type is \"{3}\". Key is \"{4}\".",
                           vm.InstanceId, vm.State, vm.Region, vm.InstanceType, vm.KeyName);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-25
            • 1970-01-01
            • 2020-05-22
            • 1970-01-01
            相关资源
            最近更新 更多