C# 这个初始化器真的是多余的吗?

发布于 2024-08-17 08:10:34 字数 474 浏览 9 评论 0原文

我有以下代码行:

var dmrReceived = new DownloadMessagesReport();

StyleCop 和 ReSharper 建议我删除冗余的初始值设定项。但是,如果我将其替换为

DownloadMessagesReport dmrReceived;

“肯定”,这将生成一个未设置为对象实例的对象引用?我正在使用.NET 3.5。您不再需要手动实例化对象吗?

接下来的下一行是:

dmrReceived = dc.DownloadNewMessages(param, param2, param3);

值得注意的是,dc 是从 WCF 服务生成的类。因此 DownloadNewMessages 是一个 WCF Web 服务方法。

I have the following line of code:

var dmrReceived = new DownloadMessagesReport();

StyleCop and ReSharper are suggesting I remove the redundant initializer. However if I replace it with

DownloadMessagesReport dmrReceived;

surely this will generate an object reference not set to an instance of an object? I am using .NET 3.5. Do you no longer manually have to instantiate objects?

Next line that follows is:

dmrReceived = dc.DownloadNewMessages(param, param2, param3);

It's worth noting that dc is a class generated from a WCF service. So DownloadNewMessages is a WCF web service method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暮倦 2024-08-24 08:10:34

如果它是一个字段,它将自动初始化为其默认值 - 对于引用类型,null。然而,鉴于 var ,我猜它不是,而且您实际上是在代码中进一步实例化它,从而丢弃您在此处实例化的值。您不需要在声明变量的地方对其进行初始化。如果您想使用 var ,您可以这样做,但我建议您在实际第一次使用它的地方声明它。

If it's a field, it will be automatically initialised to its default value - null for a reference type. Given the var however, I'm guessing it's not, and that you're actually instantiating it further down in your code anyway, thereby discarding the value you have instantiated here. You don't need to initialise a variable where it's declared. If you want to use var you do, but then I'd recommend you declare it where you actually first use it.

逐鹿 2024-08-24 08:10:34

所以您的代码是

var dmrReceived = new DownloadMessagesReport();
dmrReceived = dc.DownloadNewMessages(param, param2, param3);

第二行不会填充您在第一行中创建的对象,但它完全替换该对象。因此不需要第一个赋值(因为第一个对象从未被使用过),这正是 R# 所警告的。

So your code is

var dmrReceived = new DownloadMessagesReport();
dmrReceived = dc.DownloadNewMessages(param, param2, param3);

The second line does not fill the object you created in the first line but it completely replaces that object. So the first assignment is not needed (as the first object is never used), which is what R# is warning about.

乞讨 2024-08-24 08:10:34

如果dmrReceived在分配之前被访问,那么只会生成对象引用错误。很多时候,resharper 说初始化器是多余的原因是变量总是会在每个可能的执行路径中被分配另一个值。

DownloadMessagesReport dmrReceived;

...

if(condition) {
    dmrReceived = new DownloadMessagesReport();
} else {
    throw new Exception("oh no");
}

return dmrReceived.SomeProperty;

访问 SomeProperty 是代码中 dmrReceived 实际上需要有值的第一个位置。如下代码的其余部分所示,如果不为其分配值,则无法访问该行代码,因此,可能已分配的初始值不会在任何执行路径中使用,因此是多余的。

That will only generate an object reference error if dmrReceived is accessed before it is assigned. A lot of the times, the reason for resharper saying that an initializer is redundant is that the variable will always be assigned another value in every single possible execution path.

i.e.

DownloadMessagesReport dmrReceived;

...

if(condition) {
    dmrReceived = new DownloadMessagesReport();
} else {
    throw new Exception("oh no");
}

return dmrReceived.SomeProperty;

Accessing SomeProperty is the first place in the code where dmrReceived actually needs to have a value. As follows from the rest of the code, there's no way to get to that line of code without assigning it a value, therefore, the initial value that might have been assigned, would not be used in any execution path, and would thus be redundant.

陌上青苔 2024-08-24 08:10:34

“您是否不再需要手动
实例化对象?”

当然,你需要“手动”实例化对象,否则编译器如何知道何时何地实例化它?

一个简单的场景是这样的:

MyType x;

if ( EverythingWorkedOut )
    x = new MyType(params);
else
    x = null;

如果编译器第一次实例化它,它将是多余的,并且开销更大在所有代码中,

不要相信 ReSharper 或任何其他计算机智能的东西而不是你自己的本能!你知道它们并不总是正确的

,你真的不需要这样做 。 ; 因为它应该是非实例化对象的默认值。

"Do you no longer manually have to
instantiate objects?"

Of course you need to "manually" instantiate objects, how would the compiler know when or where to instantiate it otherwise?

A simple scenario is this:

MyType x;

if ( EverythingWorkedOut )
    x = new MyType(params);
else
    x = null;

If the compiler instantiated it the first time, it would be redundant and more overhead in all code.

Don't trust ReSharper or any other Computer-Intelligent-Stuff over your own Instincts! They're not always right you know.

Just a side note, you don't really need to do x = null; since it should be the default value of a non-instantiated object.

无悔心 2024-08-24 08:10:34

假设这是您的代码:

var dmrReceived = new DownloadMessagesReport();
dmrReceived = dc.DownloadNewMessages(param, param2, param3);

您正在第一行中创建 DownloadMessagesReport 的实例。然后,您可以通过为 dmrReceived 变量分配另一个从 DownloadNewMessages 方法返回的值来丢弃该对象。第一个 new DownloadMessagesReport() 对象是多余的。您实际上创建了垃圾收集器必须在某个时候清理的垃圾。

这就是 ReSharper 和 StyleCop 向您显示警告的原因。

如果您可以在声明变量的同一行中用实际值初始化变量,那么就这样做。

Supposing this is your code:

var dmrReceived = new DownloadMessagesReport();
dmrReceived = dc.DownloadNewMessages(param, param2, param3);

You are creating an instance of a DownloadMessagesReport in the first line. And then you throw this object away by assigning the dmrReceived variable another value returned from DownloadNewMessages method. The first new DownloadMessagesReport() object is redundant. You effectively creating garbage that Garbage Collector will have to clean at some point.

That's why ReSharper and StyleCop showing you warning.

If you can initialize variable with actual value right in the same line where the variable is declared then do it.

心安伴我暖 2024-08-24 08:10:34

当然这已经足够了吗?

DownloadMessagesReport dmrReceived = dc.DownloadNewMessages(param, param2, param3);

Surely this is enough?

DownloadMessagesReport dmrReceived = dc.DownloadNewMessages(param, param2, param3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文