【问题标题】:What does {0} stands for in Console.WriteLine?Console.WriteLine 中的 {0} 代表什么?
【发布时间】:2013-10-23 16:56:36
【问题描述】:

给定代码:

// person.cs
using System;

// #if false

class Person
{
    private string myName = "N/A";
    private int myAge = 0;

    // Declare a Name property of type string:
    public string Name
    {
        get
        {
            return myName;
        }
        set
        {
            myName = value;
        }
    }

    // Declare an Age property of type int:
    public int Age
    {
        get
        {
            return myAge;
        }
        set
        {
            myAge = value;
        }
    }

    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }

    public static void Main()
    {
        Console.WriteLine("Simple Properties");

        // Create a new Person object:
        Person person = new Person();

        // Print out the name and the age associated with the person:
        Console.WriteLine("Person details - {0}", person);

        // Set some values on the person object:
        person.Name = "Joe";
        person.Age = 99;
        Console.WriteLine("Person details - {0}", person);

        // Increment the Age property:
        person.Age += 1;
        Console.WriteLine("Person details - {0}", person);
    }
}

// #endif

代码的输出是:

Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100

Console.WriteLine("Person details - {0}", person); 中的{0} 代表什么? 怎么换成了Name.....

当我输入 {1} 而不是 {0} 时,我得到了一个例外......

【问题讨论】:

标签: c# console console.writeline


【解决方案1】:

如您所见,您的 person 对象上有一个返回字符串的代码,控制台检查您的对象类上是否存在名称为 ToString 的字符串类型,如果存在则返回您的字符串:

public override string ToString()
{
     return "Name = " + Name + ", Age = " + Age;
}

并且 {0} 是格式化的消息,当您将其定义为 {0} 时,这意味着打印/格式化您插入到函数的 params 参数中的零索引对象。这是一个从零开始的数字,用于获取您想要的对象的索引,这是一个示例:

Console.WriteLine("{0} Is great, {1} Do you think of It? {2} Think {0} Is great!", "C#", "What", "I");

// C# Is great, What do you think of It? I think C# Is great!

当您说 {0} 时,它会获取 C# 或您的对象 [] 的 [0]。

【讨论】:

    【解决方案2】:

    参数的索引号。 例如,如果你想打印多个变量,你可以这样做:

    Console.WriteLine("Person details - {0} {1}", person1, person2)
    

    【讨论】:

      【解决方案3】:

      Write/WriteLine调用的第一个参数是格式字符串。格式说明符用大括号括起来。每个说明符都包含对需要“插入”格式字符串以替换所述说明符的参数的数字引用,以及相应数据项的可选格式说明。

      Console.WriteLine(
          "You collected {0} items of the {1} items available.", // Format string
          itemsCollected,                                        // Will replace {0}
          itemsTotal                                             // Will replace {1}
      );
      

      这类似于 C/C++ 中的printf,其中%... 指定参数列表中相应项目的格式。一个主要区别是printf 系列函数要求参数及其格式说明符以相同的顺序列出。

      【讨论】:

        【解决方案4】:

        它是指给定变量(第一个变量,第二个变量等)的索引,您要写入其内容。您只有一个变量 (person),这就是为什么您只能设置第一个索引 (0)。

        Console.WriteLine("Person details - {0}", person); 中,{0} 表示:将第一个变量的内容写入{0} 所在的位置。

        【讨论】:

          【解决方案5】:

          了解这些内容的最佳方式是本网站的示例部分:

          Console.WriteLine Method (String, Object)

          【讨论】:

            猜你喜欢
            • 2018-05-08
            • 2019-12-23
            • 2014-01-05
            • 2023-01-08
            • 2015-02-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-02-02
            相关资源
            最近更新 更多