C#插值字符串中的可选参数是什么?

时间:2022-03-09 10:56:44

Interpolated strings is one of the new features of C# 6.0.

插值字符串是C#6.0的新功能之一。

According to MSDN, the syntax of the embedded C# expressions can contain an optional, comma-separated value, deemed as <optional-comma-field-width> in the documentation.

根据MSDN,嵌入式C#表达式的语法可以包含一个可选的逗号分隔值,在文档中视为

Unfortunately I didn't find what this field is for.

不幸的是,我没有找到这个领域的用途。

From its name one might think that this value sets the maximal size of the "interpolated" field, but when I try the following expression:

从它的名称可以认为这个值设置了“插值”字段的最大大小,但是当我尝试以下表达式时:

var p = Process.GetCurrentProcess();
Console.WriteLine($"Process name is {p.ProcessName, 5}");

I get the following output:

我得到以下输出:

Process name is LINQPad.UserQuery

2 个解决方案

#1


37  

It's the width to use for that field. Since your string is longer than the 5 characters you specify for the width, the field is extended to the length of your string. You'll see the difference more dramatically with a longer width:

它是用于该字段的宽度。由于您的字符串长于您为宽度指定的5个字符,因此该字段将扩展为字符串的长度。你会看到更长的宽度更显着的差异:

var p = Process.GetCurrentProcess();
$"Process name is {p.ProcessName, 50}".Dump();

yields:

收益率:

Process name is                                  LINQPad.UserQuery

A positive field size is right-justified; a negative field size is left-justified.

正字段大小是右对齐的;负字段大小是左对齐的。

The documentation is better on the Composite Formatting page of MSDN:

MSDN的“复合格式”页面上的文档更好:

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

可选的对齐组件是一个有符号整数,表示首选的格式化字段宽度。如果alignment的值小于格式化字符串的长度,则忽略alignment,并将格式化字符串的长度用作字段宽度。如果对齐为正,则字段中的格式化数据右对齐;如果对齐为负,则对齐左对齐。如果需要填充,则使用空格。如果指定了对齐,则需要逗号。

#2


14  

The number is the alignment, documented in the Alignment Component here.

数字是对齐,在此处对齐组件中记录。

The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative.

如果对齐为正,则字段中的格式化数据右对齐;如果对齐为负,则对齐左对齐。

In your example, alignment will pad the p.ProcessName with spaces if it is less than 5 characters long. Where string length is less than the absolute value of alignment (like in your example), alignment has no effect.

在您的示例中,如果长度小于5个字符,对齐将使用空格填充p.ProcessName。如果字符串长度小于对齐的绝对值(如示例中所示),则对齐无效。

Example

var text = "MyText";
Console.WriteLine($"x{text}x");
Console.WriteLine($"x{text, 3}x");
Console.WriteLine($"x{text, 10}x");
Console.WriteLine($"x{text, -10}x");

Result

结果

xMyTextx
xMyTextx
x    MyTextx
xMyText    x

#1


37  

It's the width to use for that field. Since your string is longer than the 5 characters you specify for the width, the field is extended to the length of your string. You'll see the difference more dramatically with a longer width:

它是用于该字段的宽度。由于您的字符串长于您为宽度指定的5个字符,因此该字段将扩展为字符串的长度。你会看到更长的宽度更显着的差异:

var p = Process.GetCurrentProcess();
$"Process name is {p.ProcessName, 50}".Dump();

yields:

收益率:

Process name is                                  LINQPad.UserQuery

A positive field size is right-justified; a negative field size is left-justified.

正字段大小是右对齐的;负字段大小是左对齐的。

The documentation is better on the Composite Formatting page of MSDN:

MSDN的“复合格式”页面上的文档更好:

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

可选的对齐组件是一个有符号整数,表示首选的格式化字段宽度。如果alignment的值小于格式化字符串的长度,则忽略alignment,并将格式化字符串的长度用作字段宽度。如果对齐为正,则字段中的格式化数据右对齐;如果对齐为负,则对齐左对齐。如果需要填充,则使用空格。如果指定了对齐,则需要逗号。

#2


14  

The number is the alignment, documented in the Alignment Component here.

数字是对齐,在此处对齐组件中记录。

The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative.

如果对齐为正,则字段中的格式化数据右对齐;如果对齐为负,则对齐左对齐。

In your example, alignment will pad the p.ProcessName with spaces if it is less than 5 characters long. Where string length is less than the absolute value of alignment (like in your example), alignment has no effect.

在您的示例中,如果长度小于5个字符,对齐将使用空格填充p.ProcessName。如果字符串长度小于对齐的绝对值(如示例中所示),则对齐无效。

Example

var text = "MyText";
Console.WriteLine($"x{text}x");
Console.WriteLine($"x{text, 3}x");
Console.WriteLine($"x{text, 10}x");
Console.WriteLine($"x{text, -10}x");

Result

结果

xMyTextx
xMyTextx
x    MyTextx
xMyText    x