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

What does {0} stands for in Console.WriteLine?(Console.WriteLine 中的 {0} 代表什么?)
本文介绍了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} 代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)