Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does {{{0}}} on string.Format do?

In the namespace MS.Internal, there is a class named NamedObject.

It has a weird block of code:

public override string ToString() {   if (_name[0] != '{')   {     // lazily add {} around the name, to avoid allocating a string      // until it's actually needed     _name = String.Format(CultureInfo.InvariantCulture, "{{{0}}}", _name);   }    return _name; } 

I'm curious about this comment specifically:

    // lazily add {} around the name, to avoid allocating a string      // until it's actually needed     _name = String.Format(CultureInfo.InvariantCulture, "{{{0}}}", _name); 

How is that 'lazy'? What does it do to be lazy?


Full class from the reference source:

//----------------------------------------------------------------------------  // // <copyright file="NamedObject.cs" company="Microsoft"> //    Copyright (C) Microsoft Corporation.  All rights reserved. // </copyright>  // // Description: Placeholder object, with a name that appears in the debugger  //  //---------------------------------------------------------------------------  using System; using System.Globalization; using MS.Internal.WindowsBase;  namespace MS.Internal {   /// <summary>    /// An instance of this class can be used wherever you might otherwise use   /// "new Object()".  The name will show up in the debugger, instead of    /// merely "{object}"   /// </summary>   [FriendAccessAllowed]   // Built into Base, also used by Framework.   internal class NamedObject   {     public NamedObject(string name)     {       if (String.IsNullOrEmpty(name))         throw new ArgumentNullException(name);        _name = name;     }      public override string ToString()     {       if (_name[0] != '{')       {         // lazily add {} around the name, to avoid allocating a string          // until it's actually needed         _name = String.Format(CultureInfo.InvariantCulture, "{{{0}}}", _name);       }        return _name;     }      string _name;   } }  // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. 
like image 253
Mafii Avatar asked Jul 18 '16 12:07

Mafii


People also ask

What does string format do?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.

How do you use 0 in a string?

The {0} in the format string is a format item. 0 is the index of the object whose string value will be inserted at that position. (Indexes start at 0.) If the object to be inserted is not a string, its ToString method is called to convert it to one before inserting it in the result string.

What does 0 mean in C sharp?

The “0” custom specifier is a zero placeholder. If the value to be formatted has a digit in the position where the zero appears in the format string, the the digit is copied to the resultant string.

What does string format do C#?

In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.


1 Answers

You escape a curly brace with a curly brace, i.e. {{ produces {, and }} produces }.

The {0} in the middle is interpreted as usual - i.e. a reference to parameter at index zero.

{{ {0} }} ^^ ^^^ ^^ |   |  | |   |  +--- Closing curly brace |   +------ Parameter reference +---------- Opening curly brace 

The end result is the value of parameter zero enclosed in curly braces:

var res = string.Format("{{{0}}}", "hello"); // produces {hello} 

How is that 'lazy'?

They call it lazy with respect to this alternative "eager" implementation:

internal class NamedObject {     public NamedObject(string name) {         if (String.IsNullOrEmpty(name))             throw new ArgumentNullException(name);         if (name[0] != '{') {             // eagerly add {} around the name             _name = String.Format(CultureInfo.InvariantCulture, "{{{0}}}", name);         } else {             _name = name;         }     }     public override string ToString() {         return _name;     }     string _name; } 

This implementation adds curly braces right away, even though it has no idea that the name enclosed in curly braces is going to be needed.

like image 197
Sergey Kalinichenko Avatar answered Sep 22 '22 05:09

Sergey Kalinichenko