在一个类方法中看到这一行,我的第一反应是嘲笑编写它的开发人员..但是,我认为我应该确保我是对的.
public void dataViewActivated(DataViewEvent e) {
if (this != null)
// Do some work
}
Run Code Online (Sandbox Code Playgroud)
那条线路会被评估为假吗?
请考虑以下代码:
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var square = new Square(4);
Console.WriteLine(square.Calculate());
}
}
class MathOp
{
protected MathOp(Func<int> calc) { _calc = calc; }
public int Calculate() { return _calc(); }
private Func<int> _calc;
}
class Square : MathOp
{
public Square(int operand)
: base(() => _operand * _operand) // runtime exception
{
_operand = operand;
}
private int _operand;
}
}
Run Code Online (Sandbox Code Playgroud)
(忽略课堂设计;我实际上并没有写一个计算器!这段代码只是代表了一个需要一段时间才能缩小范围的更大问题的最小代表)
我希望它能:
相反,我得到了一个无意义的异常抛出指定的行.在3.0 CLR上,它是一个NullReferenceException ; 在Silverlight …
为什么integer == nullC#中的有效布尔表达式,如果integer(类型的变量int)不可为空?(我不是反对它,事实上我喜欢它,但我不知道它是可能的)
这是允许的:
Public Property Text() As String
Run Code Online (Sandbox Code Playgroud)
而对于只读属性,为什么我不允许等效?
Public ReadOnly Property Text() As String
Run Code Online (Sandbox Code Playgroud)
我似乎被迫使用:
Public ReadOnly Property Text() As String
Get
Return fText
End Get
End Property
Run Code Online (Sandbox Code Playgroud)