bool isChanged = false;The isChanged variable is declared as type boolean and you can only store a true or false into the variable. If you try to store any other type into the variable, the compiler flags it as an error.
x = 1 x = "Hello" x = .t.In C#, when you declare a variable to be of a particular type, you can store a value of that type, OR any of its subclasses. For example, if I have a class named BusinessObject and a subclass of BusinessObject called Inventory, I can declare a variable of type BusinessObject and store a reference to a BusinessObject instance or an Inventory instance:
BusinessObject b; b = new BusinessObject(); // Store a reference to a BusinessObject b = new Inventory(); // Store a reference to an Inventory object which is a subclass of BusinessObjectIn .NET, everything is an object--even basic data types like boolean, and integer--and all classes can trace their heritage back to the Object class. This means you can declare a variable to be of type Object and you can store any value into that variable. For example:
object o; o = 1; o = "Hello"; o = true; o = new BusinessObject()Although it might seem convenient to declare ALL of your variables as type Object so you can have the freedom to store a value of any type into them, it's not a best practice, because you are throwing strong typing out the window. Strong typing is good, because the simple act of declaring the type of a variable allows the compiler to find many of your bugs--invalid values passed as parameters, invalid values returned from methods, inconsistent values returned from methods (did you ever create a method where one code path--such as an if statement--returned a value, but the other didn't???), and so on.
string[] customerIDs = new string[3]; customerIDs[0] = "ACME"; customerIDs[1] = "PICCO"; customerIDs[2] = "QUICK";The good thing about arrays in C# is they are strongly typed. The bad thing about arrays is they are not "growable". You can't redimension an array in C#.
ArrayList kidsAges = new ArrayList(); customerIDs.Add("ACME"); customerIDs.Add("PICCO"); customerIDs.Add("QUICK");You can use the Add() or Remove() method of an ArrayList at run time to add or remove items, which makes them "growable" (or "shrinkable").
string customerID = (string)customerIDs[0];A problem arises when you cast an ArrayList value to the wrong type. The following code tries to convert the customer ID to an integer.
int customerID = (int)customerIDs[0];The compiler won't catch this error because the ArrayList members are of type Object and it's possible for an ArrayList member to be of any type. However, at runtime, you will get a "Type mismatch" exception because the ArrayList element contains a string and cannot be converted to an integer.
List<string> customerIDs = new List<string>(); customerIDs.Add("ACME"); customerIDs.Add("PICCO"); customerIDs.Add("QUICK");The angle brackets are used in this declaration to specify the Type of members in the List--in this case, String. When you retrieve a value from the collection, there is no need to perform a type conversion, because the type is specified when you declare the List:
string customerID = customerIDs[0];
And the size of the List can be changed at runtime by using the Add() and Remove() methods.