伙计们,我正在测试 GeekBrains 的输入。这是免费的编程课程。老实说,我对问题的复杂性感到震惊。这是其中之一,也许它对某人有用。我是一名 C# 程序员,并没有立即猜到正确答案是什么。
屏幕上指定的三行会显示什么比较结果?
public class Text
{
public string Value1 { get; } = new string("Hello");
public string Value2 => new string("Hello");
public string Value3 => "Hello";
}
class Program
{
static void Main(string[] args)
{
Text text = new Text();
Console.WriteLine(ReferenceEquals(text.Value1, text.Value1));
Console.WriteLine(ReferenceEquals(text.Value2, text.Value2));
Console.WriteLine(ReferenceEquals(text.Value3, text.Value3));
}
}
是的,代码适用于 C# 8.0 及更高版本,.netCore 3.1
回答对,错,对
这是因为如果 objA 参数与 objB 参数匹配相同的实例,或者两者都为 null,则 Object.ReferenceEquals(Object, Object) 将返回 true;否则为假。
排队
public string Value2 => new string("Hello");
创建一个新实例。这就是为什么它是错误的。
请用 lambda 表达式解释这是一种什么样的构造?我第一次看到这个。为什么需要这样的设计以及何时使用?当我们访问一个类字段时,总是会调用 lambda 表达式并计算值。一些废话。