0

Run into term Anti-patterns in programming, and one of examples was this.

IList values = new ArrayList(); //Anti-pattern, bad
ArrayList values = new ArrayList(); //good

What is the difference between this two variants? Aren't they the same, same as with using var keyword?

Community
  • 1
  • 1
GraphicsToBe
  • 115
  • 4
  • 6
    Those are both terrible. It should be `var values = new List()` – Mike Caron Mar 18 '13 at 21:28
  • Where did you get this from? – Andrey Mar 18 '13 at 21:28
  • 4
    I'm not convinced that `List values = new ArrayList();` will compile in C#. Is this Java? – Matthew Watson Mar 18 '13 at 21:29
  • 1
    Was the example from an old Java version? It's strange to see any sort of `List` without generics (``), and there's no type `List` in .Net (just `List` and `ArrayList`). – Tim S. Mar 18 '13 at 21:29
  • It won't lol don't know up upvoted the incoreect example – MethodMan Mar 18 '13 at 21:30
  • if you want to use the `var Keyword` you need to do either of these 2 things `var values = new List();` or var values = new ArrayList();` also make sure you reference `System.Collection` – MethodMan Mar 18 '13 at 21:32
  • `ArrayList` implements *no* generic types... – Peter Ritchie Mar 18 '13 at 21:33
  • I would personally using `List` because you can always to .ToArray() on the collection if needed ... – MethodMan Mar 18 '13 at 21:34
  • If this were legal C# (and it isn't), the first would not be an antipattern. Taking this question as *really* sourced from Java, it in fact seems to be turning the premise on its head. See this related question: http://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-prefered – Anthony Pegram Mar 18 '13 at 21:50

3 Answers3

4

I assume by List you really mean IList.

IList values = new ArrayList(); just lets you view the ArrayList object as an IList object. You could cast back to ArrayList if you wanted.

ArrayList values = new ArrayList() is the same as var values = new ArrayList()

Neither is really an anti-pattern. If all you need is the methods that IList provides, it's considered good practice to only declare the type of object you need to use, regardless of what you assign it. This is more important in the public interface of a type. As a local variable; it doesn't really matter either way.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
0

In this isolation, it is mostly irrelevant.

List values = new ArrayList();
ArrayList values = new ArrayList();
var values = new ArrayList();

because here, values is declared inside a method body, and we do not care much about abstraction or isolation.

But I agree, that assigning to a List just puts a restriction to the use of values that has no advantage. One might say, this is an AnitPattern, but there are much more relevant AntiPatterns.

ArrayList and var are better, var will most often be most readable and maintainable. I personally would use var.

As an aside, hardly anyone uses these collections anymore, ArrayList is sometimes prefered to List, but I personally use the latter also here.

citykid
  • 9,916
  • 10
  • 55
  • 91
0

The advantage with IList is there are many different classes that can be assigned to it. This can be handy if you're sourcing data from different places that use different classes.

IList values;
values = new ArrayList();
values = new object[] {};
values = new List<int>();
values = new DataView();

However, if you use an IList, you can only use the methods defined by an IList. If you define the variable as an ArrayList or any other concrete class, you have access to all of that class's methods.

ArrayList values = new ArrayList();
values.

Using the var keyword will tell the compiler to use the same class as the result of the expression. It can be very useful if you have a very long-winded class.

var values = new ArrayList();
// values is ArrayList

// Assuming a function: List<int> GetIntegerList() { ... }
var values = GetIntegerList();
// values is List<int>

// Assuming a function: Dictionary<string, Dictionary<string, List<Tuple<int, int, string>>>> GetSettingsCollection() { ... }
var values = GetSettingsCollection();
// values is Dictionary<string, Dictionary<string, List<Tuple<int, int, string>>>>
Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80