相关疑难解决方法(0)

Java中的"this"可以为null吗?

在一个类方法中看到这一行,我的第一反应是嘲笑编写它的开发人员..但是,我认为我应该确保我是对的.

public void dataViewActivated(DataViewEvent e) {
    if (this != null)
        // Do some work
}
Run Code Online (Sandbox Code Playgroud)

那条线路会被评估为假吗?

java this

108
推荐指数
4
解决办法
2万
查看次数

C# - 关闭初始化程序中的类字段?

请考虑以下代码:

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)

(忽略课堂设计;我实际上并没有写一个计算器!这段代码只是代表了一个需要一段时间才能缩小范围的更大问题的最小代表)

我希望它能:

  • 打印"16",或
  • 如果在此方案中不允许关闭成员字段,则抛出编译时错误

相反,我得到了一个无意义的异常抛出指定的行.在3.0 CLR上,它是一个NullReferenceException ; 在Silverlight …

c# lambda constructor closures initialization

12
推荐指数
2
解决办法
1013
查看次数

为什么整数== null是C#中有效的布尔表达式?

为什么integer == nullC#中的有效布尔表达式,如果integer(类型的变量int)不可为空?(我不是反对它,事实上我喜欢它,但我不知道它是可能的)

c# null boolean non-nullable

11
推荐指数
1
解决办法
631
查看次数

为什么我不能自动实现只读属性

这是允许的:

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)

.net vb.net

5
推荐指数
1
解决办法
1674
查看次数

标签 统计

c# ×2

.net ×1

boolean ×1

closures ×1

constructor ×1

initialization ×1

java ×1

lambda ×1

non-nullable ×1

null ×1

this ×1

vb.net ×1