Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the optional argument in C# interpolated string for?

Interpolated strings is one of the new features of 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.

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 
like image 399
Alexander Galkin Avatar asked Sep 17 '15 00:09

Alexander Galkin


People also ask

What is optional argument?

Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates. When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.

Does C have optional arguments?

Optional arguments are generally not allowed in C (but they exist in C++ and in Ocaml, etc...). The only exception is variadic functions (like printf ).

What is optional in a function declaration in C?

Parameter declarations are optional. Functions that take no parameters are written without parentheses. The function body begins with the keyword IS (or AS ) and ends with the keyword END followed by an optional function name.

How do you make an optional argument?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.


1 Answers

It's the minimum width to use for that field, not the maximum. 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:

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:

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.

like image 186
D Stanley Avatar answered Sep 23 '22 09:09

D Stanley