/* CSC 123 C# Warm up assignment: due Tuesday 10/25 Part I: C# is basically like Java, but there are some unique features. One of these is the "delegate" construct, which allows you to write higher-order functions. That is, functions that take other functions as arguments (such as map, fold, howmany, etc ...). Study the sample program on the homepage (delegates.cs) to understand how it works. Then write the delegate public delegate bool boolFun(int x); And implement your favourite scheme/perl function "howmany" on arrays. That is, it should return the number of elements in an array for which the delegate function returns true. Demonstrate your program with at least two instantiations of the the delegate template. Part II: The .Net API contains a built-in class called ArrayList, which works like the array/list structures in Perl. Go to the MSDN website and search for "ArrayList" for the complete docs, but the following example should be more than enough. The structure can be used like a list or an array. As the example indicates, you can store data of different types in the same ArrayList. That's because both strings and integers are implicitly type-cast to the ultimate supertype "object". As far as the compiler is concerned, each element of the ArrayList is an object. You cannot say A[3]+2 even if you know that A[3] is an integer - because all the compiler knows is that it's an object. Before you can use the element as an integer, you must explicitly type-cast it back to an int. Study the small program to make sure you understand this point. Remove the type-cast (int) operator in the program and observe what happens. Does it even compile? What if you try to use A[1] ("world") as an integer? That is, what if you try to add 2 to A[1] by first casting it to an integer? What kind of error do you get (compile time or runtime?) Record the behavior and EXPLAIN WHY. Finally, adopt the "howmany" higher-order function to work with ArrayLists instead of simple arrays. Remember to type-cast before you use the elements as integers. */ using System; using System.Collections; // needed to use ArrayList public class cshw1 { public static void Main() { ArrayList A = new ArrayList(); // creates empty array list A.Add("hello"); // add works like "push" - adds to end of list A.Add("world"); A.Add(30); A.Add("blah"); Console.WriteLine("there are "+A.Count+" objects in the ArrayList"); A[3] = (int)A[2] + 1; // what will happen if there's no (int) ? A.Remove("blah"); // removes the specified element foreach (object x in A) { Console.WriteLine(x); } // Microsoft is so smart! where did they get the idea for a foreach loop? } // Main } // Compile this program with csc cshw1.cs and run it with ./cshw1