0

我找到了这个符号{0},这个符号是什么意思?

4

5 回答 5

3

它最常用作字符串格式化函数的一部分,这意味着(从零开始的)列表中的第一个参数应该替换它。例如:

var output = String.Format("{0},{1}", "Hello", "World") // Gives "Hello, World"

字符串格式化是数据绑定中的常见元素,因此您也经常将其视为绑定表达式的一部分。

于 2012-09-25T07:27:30.850 回答
1

它是一个字符串替换标记。

看看这个例子,它解释了这些符号的使用:

class Program
{
    static void Main()
    {
    string value1 = "Dot";
    string value2 = "Net";
    string value3 = "Perls";

    Console.WriteLine("{0}, {1}, {2}", // <-- This is called a format string.
        value1,                        // <-- These are substitutions.
        value2,
        value3);
    }
}

这会导致输出:

点、网、Perls

于 2012-09-25T07:27:17.233 回答
0

它可用于字符串格式化

DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0); 
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                              dat, city, temp);
Console.WriteLine(output);
// The example displays the following output: 
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.   
于 2012-09-25T07:27:28.307 回答
0

它是一个占位符(示例):

int selectedItem = 1;

// Generate the output string
string output = string.Format("You selected item {0} from the list.", selectedItem);

Console.WriteLine(output);     // Outputs "You selected item 5 from the list."
于 2012-09-25T07:29:06.170 回答
0

它是一个复合格式字符串中的从零开始的索引占位符,称为格式项。

在运行时,每个格式项都替换为参数列表中相应参数的字符串表示形式。如果参数的值为 null,则将其替换为String.Empty

例如,以下对 Format(String, Object, Object, Object) 方法的调用包括一个具有三个格式项 {0}、{1} 和 {2} 的格式字符串,以及一个具有三个项的参数列表。

详细的格式化帮助可以在http://msdn.microsoft.com/en-us/library/txafckwd.aspx找到

于 2012-09-25T07:30:15.030 回答