给出以下示例代码:
enum op
{
add,
remove
}
Func<op, int> combo(string head, double tail) =>
(op op) =>
op == op.add ? Int32.Parse(head) + Convert.ToInt32(tail) : Int32.Parse(head) - Convert.ToInt32(tail);
Console.WriteLine(combo("1", 2.5)(op.remove));
Run Code Online (Sandbox Code Playgroud)
哪个返回:
-1
第一个箭头运算符是什么意思?
根据规范,它看起来不像表达式主体或lambda运算符。
有没有在任何参考C#语言规范有关此用法?
我的其中一个属性如下所示:
public string Name
{
get{ return _name; }
set { _name = value; }
}
Run Code Online (Sandbox Code Playgroud)
但是ReSharper建议我将其更改为:
public string Name
{
get => _name;
set => _name = value;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样重构,则编译会引发错误是否无法在Property中包含表达式主体?
lambda 表达式是被视为对象的代码块(表达式或语句块)。它可以作为参数传递给方法,也可以通过方法调用返回。
(input parameters) => expression
SomeFunction(x => x * x);
Run Code Online (Sandbox Code Playgroud)
看着这个语句,我想知道使用 lambdas 和使用 Expression-bodied 时有什么区别?
public string Name => First + " " + Last;
Run Code Online (Sandbox Code Playgroud) 当我使用时
var frontPage = await GetFrontPage();
protected override async Task<WordDocument> GetFrontPage()
{
return null;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,我在 frontpage 变量中得到空值。但是当我将函数重写为
protected override Task<WordDocument> GetFrontPage() => null;
Run Code Online (Sandbox Code Playgroud)
我收到了一个NullReferenceException
.
谁能帮助我理解这两种说法之间的区别?