string.Format上有{{{0}}}做什么?

Maf*_*fii 55 c# lazy-evaluation

在命名空间中MS.Internal,有一个名为的类NamedObject.

它有一个奇怪的代码块:

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;
}
Run Code Online (Sandbox Code Playgroud)

我特别好奇这个评论:

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

那个'懒惰'怎么样?懒惰是做什么用的?


从满级的参考来源:

//---------------------------------------------------------------------------- 
//
// <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.
Run Code Online (Sandbox Code Playgroud)

das*_*ght 66

用大括号逃脱一个花括号,即{{生产{}}生产}.

{0}在中间被解释为通常的-即,在参考索引零到参数.

{{ {0} }}
^^ ^^^ ^^
|   |  |
|   |  +--- Closing curly brace
|   +------ Parameter reference
+---------- Opening curly brace
Run Code Online (Sandbox Code Playgroud)

最终结果是用花括号括起来的参数零的值:

var res = string.Format("{{{0}}}", "hello"); // produces {hello}
Run Code Online (Sandbox Code Playgroud)

那个'懒惰'怎么样?

他们称这种替代"渴望"的实施方式是懒惰的:

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;
}
Run Code Online (Sandbox Code Playgroud)

此实现立即添加花括号,即使它不知道将需要括在花括号中的名称.


Hen*_*man 15

那个'懒惰'怎么样?懒惰是做什么用的?

懒惰来自于它if (_name[0] != '{')之前.

它仅_name在第一次请求时更改字段.

就像每个人已经指出的那样,String.Format("{{{0}}}", _name);应该被视为"{{ {0} }}""\{ {0} \}".内部{0}是实际领域的第一个参数,外替代{{}}是一个特殊的符号来获得单{}


nl-*_*l-x 10

{{并且}}只是给你文字{}.(Escaped花括号)

所以,如果你有{{{0}}},并且你放弃foo,输出将是{foo}


Bac*_*cks 6

var value = "value";
String.Format(CultureInfo.InvariantCulture, "{{{0}}}", value); // will output {value}
Run Code Online (Sandbox Code Playgroud)