{{{0}}} 对字符串有什么作用.格式做

本文关键字:作用 格式 什么 字符串 | 更新日期: 2023-09-27 17:57:23

在命名空间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;
}

我对这个评论特别好奇:

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

这怎么是"懒惰"?懒惰有什么作用?


来自参考源的完整类:

//---------------------------------------------------------------------------- 
//
// <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.

{{{0}}} 对字符串有什么作用.格式做

你用大括号逃脱了大括号,即 {{产生{}}产生}

中间的{0}像往常一样被解释 - 即对索引零处的参数的引用。

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

最终结果是括在大括号中的参数零的值:

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

这怎么是"懒惰"?

他们称其为这种替代的"渴望"实现的懒惰:

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;
}

此实现会立即添加大括号,即使它不知道需要括在大括号中的名称。

这怎么是"懒惰"?懒惰有什么作用?

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

它仅在首次请求时更改_name字段。

就像每个人都已经指出的那样,String.Format("{{{0}}}", _name);应该被解读为"{{ {0} }}""'{ {0} '}" .内{0}是用第一个参数替换的实际字段,外{{}}是获得单个{}的特殊表示法

{{}}只是给你字面上的{}。(转义的大括号)

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

var value = "value";
String.Format(CultureInfo.InvariantCulture, "{{{0}}}", value); // will output {value}