C# Is this initialiser really redundant?

后端 未结 6 547
故里飘歌
故里飘歌 2021-01-15 14:40

I have the following line of code:

var dmrReceived = new DownloadMessagesReport();

StyleCop and ReSharper are suggesting I remove the redun

相关标签:
6条回答
  • 2021-01-15 14:53

    Surely this is enough?

    DownloadMessagesReport dmrReceived = dc.DownloadNewMessages(param, param2, param3);
    
    0 讨论(0)
  • 2021-01-15 14:55

    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.

    0 讨论(0)
  • 2021-01-15 15:04

    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.

    0 讨论(0)
  • 2021-01-15 15:14

    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.

    0 讨论(0)
  • 2021-01-15 15:19

    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.

    0 讨论(0)
  • 2021-01-15 15:20

    "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.

    0 讨论(0)
提交回复
热议问题