1

What is the diference between those two examples of code?

  public Test(int x)
  {
        List<int> list= new List<int>();
        List<int> list1 = new List<int>();

        list= CreateList(x);
        list1 = CreateList(x + 1);

        DoStuff(list, list1);

        list.Clear();
        list = null;
        list1.Clear();
        list1 = null;
    } 

Is this the way to code?

    public Test(int nCount)
    {
        var list = CreateList(nCount);
        var list1 = CreateList(nCount + 1);

        DoStuff(list, list1);
    }
Rumplin
  • 2,703
  • 21
  • 45
  • You avoided typing in `List` on two ocassions – V4Vendetta Jul 12 '11 at 08:49
  • In the first example I clear the list and dispose the elements by setting them to null. With var type I don't need to do that anymore? – Rumplin Jul 12 '11 at 08:53
  • 3
    There is no such need to setting it to null and that is not called disposing .. [Read further](http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null) – V4Vendetta Jul 12 '11 at 08:55

4 Answers4

3

You're not actually comparing like with like as all var means is "let the compiler decide what type this is". It will compile to the same code (assuming that CreateList returns a List<int>). So while your code is functionally identically you're doing extra work in the first example that you're not replicating in the second.

In the first case you are creating lists unnecessarily as the act of returning a list from CreateList(x) will overwrite what you have just created. A more accurate question would be asking for this difference between:

public Test(int x)
{
    List<int> list= CreateList(x);
    List<int> list1 = CreateList(x + 1);

    DoStuff(list, list1);

    list.Clear();
    list = null;
    list1.Clear();
    list1 = null;
}

and

public Test(int nCount)
{
    var list = CreateList(nCount);
    var list1 = CreateList(nCount + 1);

    DoStuff(list, list1);
}

In both cases as the lists fall out of scope they'll be marked as eligible for garbage collection. You don't need the calls to Clear() or to set the variables to null.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
2

Nothing.

For readability and therefore future maintenance reasons, use the second one. It's clearly much easier to understand, and because of the design of C# and the CLR, much of the code in your first example is not required.

I'll try to explain what I mean. You don't need to clear or set to null any references to local variables (the garbage collector will do this for you). You only need to do this for class-level fields, and often not even then (again, the garbage collector will collect them when your class goes out of scope).

Consider focussing more on the concept of objects being disposable, and don't worry about the memory usage/garbage collection aspects. Emptying a list or setting the reference to the list to null doesn't actually reclaim memory; the memory is still allocated until the garbage collector reclaims it at some indeterminate time in the future.

There are means to force a garbage collection, but typically that means you're doing something wrong if you need to go that far. Some would say if you need to worry about memory in so much detail, .NET may not be the correct choice of language for your problem domain.

Also, using var or specifying the type explicitly makes no difference whatsoever.

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • in the first example, do I need to clear the list and set it to null? – Rumplin Jul 12 '11 at 08:52
  • 1
    Not at all, no. See my answer for a more detailed explanation, but you would only need to do this if your lists were fields on the class rather than method-level local variables, and you were worried about the values in them being accidentally re-used or used in another method that shared the fields. This would usually be a bug in any case, so I only mention it for completeness. – Neil Barnwell Jul 12 '11 at 08:56
0

The only difference that is worth mentioning is that when you clear the list manually, you're releasing references to the objects at that point. In the other version, the list will clear the references during it's disposal by the garbage collector, which can occur at any time.

That's a minor difference, because the released references will only get disposed themselves when the GC collects anyways.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

It depends on what you do with "DoStuff". If you create another reference to the list the garbage collector won't clear them in the second example. So the first example guarantees the lists are cleared and in the second it depends on "DoStuff".

I would use the following code:

DoStuff(CreateList(nCount), CreateList(nCount + 1));
MrFox
  • 4,852
  • 7
  • 45
  • 81