==到目前为止,我一直在我的程序中使用运算符来比较我的所有字符串.但是,我遇到了一个错误,将其中一个更改为了.equals(),并修复了该错误.
是==坏?什么时候应该不应该使用它?有什么不同?
在我使用的前一个团队中,每当创建一个新的Service类来处理数据层和表示层之间的业务逻辑时,就会完成以下操作:
class DocumentService
{
public DocumentRepository DocumentRepository { get; set; }
public DocumentService()
{
if (DocumentRepository == null) DocumentRepository = new DocumentRepository();
}
}
Run Code Online (Sandbox Code Playgroud)
我从来都不明白为什么检查是null在那里.如果正在调用构造函数,那意味着它必须为null ..因为它是一个新实例,对吧?
为什么要这样做?在我看来,这是一个多余的步骤,但我不想错过任何东西并将其作为不好的做法传递出去.