<legend id='0NZRK'><style id='0NZRK'><dir id='0NZRK'><q id='0NZRK'></q></dir></style></legend>

    <small id='0NZRK'></small><noframes id='0NZRK'>

      • <bdo id='0NZRK'></bdo><ul id='0NZRK'></ul>
      <tfoot id='0NZRK'></tfoot>
    1. <i id='0NZRK'><tr id='0NZRK'><dt id='0NZRK'><q id='0NZRK'><span id='0NZRK'><b id='0NZRK'><form id='0NZRK'><ins id='0NZRK'></ins><ul id='0NZRK'></ul><sub id='0NZRK'></sub></form><legend id='0NZRK'></legend><bdo id='0NZRK'><pre id='0NZRK'><center id='0NZRK'></center></pre></bdo></b><th id='0NZRK'></th></span></q></dt></tr></i><div id='0NZRK'><tfoot id='0NZRK'></tfoot><dl id='0NZRK'><fieldset id='0NZRK'></fieldset></dl></div>

      Console.WriteLine 中的 {0} 代表什么?

      时间:2023-08-27
          <tbody id='RlQJo'></tbody>

        <i id='RlQJo'><tr id='RlQJo'><dt id='RlQJo'><q id='RlQJo'><span id='RlQJo'><b id='RlQJo'><form id='RlQJo'><ins id='RlQJo'></ins><ul id='RlQJo'></ul><sub id='RlQJo'></sub></form><legend id='RlQJo'></legend><bdo id='RlQJo'><pre id='RlQJo'><center id='RlQJo'></center></pre></bdo></b><th id='RlQJo'></th></span></q></dt></tr></i><div id='RlQJo'><tfoot id='RlQJo'></tfoot><dl id='RlQJo'><fieldset id='RlQJo'></fieldset></dl></div>

            <legend id='RlQJo'><style id='RlQJo'><dir id='RlQJo'><q id='RlQJo'></q></dir></style></legend>

              <small id='RlQJo'></small><noframes id='RlQJo'>

              <tfoot id='RlQJo'></tfoot>
                <bdo id='RlQJo'></bdo><ul id='RlQJo'></ul>
                本文介绍了Console.WriteLine 中的 {0} 代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                给定代码:

                // 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..... 了?

                What does the {0} in Console.WriteLine("Person details - {0}", person); stands for ? How come it's replaced by Name..... ?

                当我使用 {1} 而不是 {0} 时,我得到一个异常...

                When I put {1} instead of {0} I get an exception ...

                推荐答案

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

                As you can see, There's a code on your person object that returns a string, Console checks for If a type of string with name of ToString exists on your object class or not, If exists then It returns your string:

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

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

                And {0} Is a formatted message, When you define It to {0} It means printing/formatting the zero Index object that you Inserted Into params arguments of your function. It's a zero based number that gets the index of object you want, Here's an example:

                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].

                When you say {0} It gets C# or the [0] of your object[].

                这篇关于Console.WriteLine 中的 {0} 代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何确定控制台应用程序是如何启动的? 下一篇:如何在 .NET 中禁止将按键打印到控制台?

                相关文章

                最新文章

                <tfoot id='SEDt2'></tfoot>
              • <i id='SEDt2'><tr id='SEDt2'><dt id='SEDt2'><q id='SEDt2'><span id='SEDt2'><b id='SEDt2'><form id='SEDt2'><ins id='SEDt2'></ins><ul id='SEDt2'></ul><sub id='SEDt2'></sub></form><legend id='SEDt2'></legend><bdo id='SEDt2'><pre id='SEDt2'><center id='SEDt2'></center></pre></bdo></b><th id='SEDt2'></th></span></q></dt></tr></i><div id='SEDt2'><tfoot id='SEDt2'></tfoot><dl id='SEDt2'><fieldset id='SEDt2'></fieldset></dl></div>

                    <bdo id='SEDt2'></bdo><ul id='SEDt2'></ul>

                  <small id='SEDt2'></small><noframes id='SEDt2'>

                  1. <legend id='SEDt2'><style id='SEDt2'><dir id='SEDt2'><q id='SEDt2'></q></dir></style></legend>