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

编程入门 行业动态 更新时间:2024-10-23 23:34:38
本文介绍了{0}在Console.WriteLine中代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定代码:

// person.cs using System; // #if false class Person { private string myName =N / A; private int myAge = 0; //声明一个类型为string的Name属性: public string Name { get { return myName; } set { myName = value; } } //声明一个类型为int的Age属性: public int Age { get { return myAge; } set { myAge = value; } } public override string ToString() { returnName =+ Name +,Age =+ Age; } public static void Main() { Console.WriteLine(Simple Properties); //创建一个新的Person对象: person person = new Person(); //打印出与该人相关联的名称和年龄: Console.WriteLine(Person details - {0},person); //在person对象上设置一些值: person.Name =Joe; person.Age = 99; Console.WriteLine(Person details - {0},person); //增加Age属性: person.Age + = 1; Console.WriteLine(Person details - {0},person); } } // #endif

代码的输出是:

简单属性个人详细信息 - Name = N / A,Age = 0 个人详细资料 - Name = Joe,Age = 99 个人详细资料 - Name = Joe,Age = 100

{0} 在 Console.WriteLine(Person详细信息 - {0},person) / code>代表? 如何替换为 Name ..... ?

当我放置 {1} 而不是 {0} 我得到一个异常...

解决方案

如你所见,有一个代码在你的person对象返回一个字符串,控制台检查如果一个类型的字符串名称为ToString存在于你的对象类上,如果存在,那么它返回你的字符串:

public override string ToString() { returnName =+ Name +,Age =+ Age;是一个格式化的消息,当您将它定义为{0}时,它将返回0。 }这意味着打印/格式化您插入到params函数的参数的零Index对象。这是一个基于零的数字,获得您想要的对象的索引,这里有一个例子:

Console.WriteLine({0}很好,{1}你会想到它吗?{2}想{0}是很好!,C#,什么,我); // C#很好,你觉得怎么样?我认为C#是伟大的!

当你说{0}它得到C#或你的对象[]的[0] / p>

Given the code :

// 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

The output of the code is :

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

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

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

解决方案

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; }

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!

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

更多推荐

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

本文发布于:2023-11-25 02:44:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代表   Console   WriteLine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!