gpt4 book ai didi

javascript - C# 有符号类型吗?

转载 作者:行者123 更新时间:2023-11-29 18:01:55 26 4
gpt4 key购买 nike

JavaScript (ES2015) 引入了新的 Symbol type ,它与字符串的不同之处在于符号是通过引用进行比较,而字符串是通过值进行比较。

C# 有什么可比较的吗?

最佳答案

C# 中的

object 满足 ECMAScript 6 中 symbol 的许多相同属性。

考虑 JavaScript 代码:

const MY_KEY = Symbol();
let obj = {};

obj[MY_KEY] = 123;
console.log(obj[MY_KEY]); // 123

在 C# 中,这可以类似地表示为:

readonly object MyKey = new object();
// ...
var obj = new Dictionary<object, int>() {{MyKey, 123}};
Console.WriteLine(obj[MyKey]); // 123

但是,Symbol 类型包含其他功能:全局注册表、众所周知的 key 以及使用描述标记符号的功能(ES2015 规范,§ 19.4)。此功能可以在 C# 6 中作为一个简单的包装器大量复制:

class Symbol : IEquatable<Symbol>
{
private object o = new object();
public string Description { get; }

public Symbol(string description)
{
Description = description;
}
public Symbol() : this(null) { }

public bool Equals(Symbol other) => other?.o == o;
public static bool operator == (Symbol o1, Symbol o2) =>
EqualityComparer<Symbol>.Default.Equals(o1, o2);
public static bool operator !=(Symbol o1, Symbol o2) => !(o1 == o2);

public override int GetHashCode()
{
return o.GetHashCode();
}
public override bool Equals(object obj)
{
return Equals(obj as Symbol);
}
public override string ToString() => $"symbol({Description})";

// static methods to support symbol registry
private static Dictionary<string, Symbol> GlobalSymbols =
new Dictionary<string, Symbol>(StringComparer.Ordinal);

public static Symbol For(string key) =>
GlobalSymbols.ContainsKey(key) ?
GlobalSymbols[key] :
GlobalSymbols[key] = new Symbol(key);

public static string KeyFor(Symbol s) =>
GlobalSymbols.FirstOrDefault(a => a.Value == s).Key; // returns null if s not defined

// Well-known ECMAScript symbols
private const string ns = "Symbol.";
public static Symbol HasInstance => For(ns + "hasInstance");
public static Symbol IsConcatSpreadable => For(ns + "isConcatSpreadable");
public static Symbol Iterator => For(ns + "iterator");
public static Symbol Match => For(ns + "match");
public static Symbol Replace => For(ns + "replace");
public static Symbol Search => For(ns + "search");
public static Symbol Species => For(ns + "species");
public static Symbol Split => For(ns + "split");
public static Symbol ToPrimitive => For(ns + "toPrimitive");
public static Symbol ToStringTag => For(ns + "toStringTag");
public static Symbol Unscopables => For(ns + "unscopables");
}

结果行为符合预期的 ECMAScript 行为:

Symbol a = new Symbol();
Symbol b = new Symbol("A");
Symbol c = new Symbol("A");
Symbol d = c;
var dict = new Dictionary<object, int>() { { c, 42 } };
Symbol e = Symbol.For("X");
Symbol f = Symbol.For("X");
Symbol g = Symbol.For("Y");
Console.WriteLine(a == b); // false
Console.WriteLine(b == c); // false
Console.WriteLine(c == d); // true
Console.WriteLine(dict[d]); // 42
Console.WriteLine(e == f); // true
Console.WriteLine(f == g); // false
Console.WriteLine(Symbol.For("X") == e); // true
Console.WriteLine(Symbol.KeyFor(e) == "X"); // true
Console.WriteLine(Symbol.Unscopables.Description); // Symbol.unscopables

关于javascript - C# 有符号类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34371287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com