1

Resharper is showing this warning: "value assigned is not used in any execution path" when I write the following code:

List<obj> testObj = new List<obj>();
testObj = testMethod();

Here testMethod() returns type List<obj>. However when I directly assign the testMethod() without instantiating it, I don't get the warning.

List<obj> testObj = testMethod();

Forgive me for my ignorance, if I am missing the basics but I'm not following how the compiler allocates memory for testObj without instantiating it.

One of the posts refers to similar question: C# Is this initialiser really redundant? but I don't find any answer to my question, as to where the testObj variable is storing the value it received from testMethod? Unlike primitive data types, 'object types' can store value only after they are instantiated. Please let me know, if I am missing something.

Community
  • 1
  • 1
Sanu
  • 105
  • 2
  • 13
  • 1
    This is indeed duplicate of question you've linked - there is no difference what type of object you instantiate (and there is no reason to create one question per possible type you can `new` instance of and than immediately ignore that value). – Alexei Levenkov Oct 14 '15 at 03:15
  • 1
    For reference types the compiler only allocates a pointer, but not the object itself. It's only when you `new` the object does the memory get allocated. – Enigmativity Oct 14 '15 at 03:24
  • @AlexeiLevenkov I have edited my question and added more details to my question and hope it now differentiates itself and not be a duplicate anymore. – Sanu Oct 14 '15 at 20:20

1 Answers1

8

You are creating an instance of a List<object> in the first line. And then you throw this object away by assigning the testObj variable another value returned from your method testMethod. This new List<obj>() object is redundant. You effectively creating garbage that Garbage Collector will have to clean at some point.

That's why ReSharper showing you warning.

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

EDIT (assuming we are talking about situation provided in the question):

  1. new operator does not deal with variable itself. It creates a new object in the heap (for reference types).
  2. = operator assigns a value to a variable in the stack. In this case value is a reference to an object in the heap.
  3. There is no difference between assigning a value to a variable returned from a method or an object constructor. Object constructor is actually a method too.
  4. Variable don't need to be initialized before it's possible to assign a value to it. Actually initialization by definition is an assignment of initial value to a variable.
Vova
  • 1,356
  • 1
  • 12
  • 26
  • I have added more details to my question now to point to my question precisely. `testMethod` is reading a list of objects from the database and returning it to `testObj` variable. So, isn't `testObj` variable required to be allocated memory before it can be assigned the return value? This is why I `new` it but ReSharper shows a warning that it wasnt required. Hope I am clearer now in what I am asking. – Sanu Oct 14 '15 at 20:22
  • See updates to the unswer. I put it there for better formatting. – Vova Oct 14 '15 at 20:52
  • `int v = new Int32();` does not create anything on the heap :) – Alexei Levenkov Oct 14 '15 at 20:54
  • This pretty much answers my question - _There is no difference between assigning a value to a variable returned from a method or an object constructor_ Just to summarize my understanding, in my code, value returned by `testMethod` is already residing in the heap and therefore there is no need to create a `new` for my variable `testObj` to hold the value. Unless, `testObj` was assigned values for the first time externally. Thanks for your explanation :) – Sanu Oct 15 '15 at 22:24