gpt4 book ai didi

c# - 字符串运算符+源代码在哪里?

转载 作者:太空宇宙 更新时间:2023-11-03 19:38:10 25 4
gpt4 key购买 nike

当我检查以下代码时 string s = "a"+ 1 有效。

在工具提示中,Visual Studio 显示它使用 字符串运算符 +(字符串左侧,对象右侧) enter image description here

无论如何,source code of String 中没有提到它,因为只有 ==!=

我绝对可以假设它使用类似于 left + right.ToString() 的东西,但真的想检查它的真实源代码。

附言让它不存在于 C# 源代码中使我的兴趣无限增加 :)

最佳答案

这对我来说是新的,我不是编译器专家,这是我最好的机会。

源码在哪里?

简答

贯穿 .NET Compiler Platform ("Roslyn") 的代码。

更长的答案

string 在 c# 中有 3 个内置运算符。

string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);

(引用下方)

您可以在 Roslyn 的 BuiltInOperators 类源代码中看到它们。

(int)BinaryOperatorKind.StringConcatenation,
(int)BinaryOperatorKind.StringAndObjectConcatenation,
(int)BinaryOperatorKind.ObjectAndStringConcatenation,

您可以按照 Kind 找到 BinaryOperatorKind 枚举。

StringConcatenation = String | Addition,
StringAndObjectConcatenation = StringAndObject | Addition,
ObjectAndStringConcatenation = ObjectAndString | Addition,

使 + 成为对 string.Concat 的调用的魔术的结束部分(在它被识别为加法表达式之后)似乎发生在 RewriteStringConcatenation 方法中。

首先将左侧和右侧转换为字符串,这可能需要调用 ToString(),您可以在其他人发布的 IL 中看到。

// Convert both sides to a string (calling ToString if necessary)
loweredLeft = ConvertConcatExprToString(syntax, loweredLeft);
loweredRight = ConvertConcatExprToString(syntax, loweredRight);

许多行之后,可能会调用 RewriteStringConcatenationNNNxprs 方法之一,例如提供特殊 string.Concat 方法的 RewriteStringConcatenationTwoExprs

private BoundExpression RewriteStringConcatenationTwoExprs(SyntaxNode syntax, BoundExpression loweredLeft, BoundExpression loweredRight)
{
Debug.Assert(loweredLeft.HasAnyErrors || loweredLeft.Type.IsStringType());
Debug.Assert(loweredRight.HasAnyErrors || loweredRight.Type.IsStringType());

var method = UnsafeGetSpecialTypeMethod(syntax, SpecialMember.System_String__ConcatStringString);
Debug.Assert((object)method != null);

return (BoundExpression)BoundCall.Synthesized(syntax, null, method, loweredLeft, loweredRight);
}

字符串运算符引用

以下内容来自 Expressions 文章的加法运算符部分,该文章是 C# 规范的一部分。

  • String concatenation:
string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);

These overloads of the binary + operator perform string concatenation. If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object. If ToString returns null, an empty string is substituted.

using System;

class Test
{
static void Main() {
string s = null;
Console.WriteLine("s = >" + s + "<"); // displays s = ><
int i = 1;
Console.WriteLine("i = " + i); // displays i = 1
float f = 1.2300E+15F;
Console.WriteLine("f = " + f); // displays f = 1.23E+15
decimal d = 2.900m;
Console.WriteLine("d = " + d); // displays d = 2.900
}
}

The result of the string concatenation operator is a string that consists of the characters of the left operand followed by the characters of the right operand. The string concatenation operator never returns a null value. A System.OutOfMemoryException may be thrown if there is not enough memory available to allocate the resulting string.


.NET 4.8 的字符串源代码位于 here,但它仅包含 operator ==operator != 的源代码。

语法可视化工具

您可能会或可能不会发现这与这个问题相关,但 Visual Studio 附带了语法可视化器( View -> 其他窗口),它提供了一个窗口来了解代码生成的一些神奇之处。

enter image description here

关于c# - 字符串运算符+源代码在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58924625/

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