C# for Java Programmers

Collections are a means of grouping objects of the same type into a structure. You may be thinking: Isn t that what arrays are? In fact, you would be correct. Arrays are a type of Collection object; the simplest type, in fact. Some disadvantages of arrays are that they do not permit us to dynamically resize them when needed. When they are initialized we specify their dimensions, and the only way we can change this is to actually reinitialize them. With Collections we can add elements to our collection object, without worrying that the collection cannot hold any more, as we would with arrays.
You have most likely used collection classes in Java such as the Vector class, and the ArrayList class. C# has its own ArrayList, which contains the capabilities of these Java classes. Both Java and C# store the items in the collection as objects, so the responsibility is on you to cast it back to its type. An advantage C# has over Java is the use of indexers to access items in a collection, as opposed to Java, which requires use of the get method. Here is a side-by-side comparison of identical code in Java and C# to create, initialize, and use an ArrayList. This code keeps track of players at our poker game.
import java.io.IOException;import java.util.*;class CollectionsDemo{ public static void main(String[] args) {<b class="bold"> ArrayList PokerPlayers = new ArrayList(3);</b> PokerPlayers.add(new String("Joe Bob")); PokerPlayers.add(new String("Mike Smith")); PokerPlayers.add(new String("Al...