c# - string.Format 上的 {{{0}}} 是做什么的?

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

我特别好奇这个评论:

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

那是怎样的“懒惰”?懒惰有什么用?


来自 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.

最佳答案

escape a curly brace with a curly brace ,即 {{ 产生 {,而 }} 产生 }

中间的 {0} 照常解释 - 即对索引零处参数的引用。

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

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

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

How is that 'lazy'?

对于这种替代的“急切”实现,他们称其为懒惰:

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

此实现会立即添加花括号,即使它不知道将需要用花括号括起来的名称。

关于c# - string.Format 上的 {{{0}}} 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436847/

相关文章:

lazy-loading - 延迟加载的反义词是什么?

c# - 无法从程序集“Microsoft.AspNetCore.Mvc.Core”加载类型 'Microsoft.AspNetCore.Mvc.Internal.IActionSelectorDecisionTreeProvider'

programming-languages - 非升降式产品的缺点?

initialization - 编写一个在初始化程序中提供自引用的 Kotlin util 函数

seo - 延迟 'lazy' 加载 YouTube 视频内容和 SEO 效果

http - 懒惰地消费http请求

c# - Microsoft.Bcl.Async 是如何工作的?

c# - 如何用不同的 stub 调用替换已声明的 stub 调用?

c# - 敏捷包后代功能问题

c# - 在 MVC5 View 中更改按钮样式