The SLAR on System.Array

The SLAR on System.Array

Continuing in the series on sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 here are some of the annotations and a sample on the System.List class.

 

BA Notice that we used signed values for the indices in the Array class. Some people may feel this is an odd choice as the negative values are always invalid for indices. There are two reasons for this. First, the Common Language Specification lacks support for unsigned 32-bit integrals, which means some languages would not be able to consume these members. Second, unsigned numbers are just harder to work with. Many domains naturally return values as signed numbers, so using unsigned indices here would lead to uglier code with more casts.

BA  Notice that the Array class implements the IList interface. We did that to support databinding and other generic mechanisms such as the “foreach” language construct in C#. Accessing elements through the IList interface on an array is significantly more expensive than accessing elements directly, because the JIT can, for example, recognize code patterns and hoist bounds checks. Luckily, the C# compiler generates special case code for arrays that the JIT is able to recognize and optimize. For example, look at the code the C# compiler generates for this:

int [] arr = new int[] {1,2,3,4};

foreach (int i in arr)

{

      Console.WriteLine (i);

}

Rather than going through the enumerator the C# compiler generates code as if you

had written:

int [] arr = new int[] {1,2,3,4};

for (int i = 0; i < arr.Length; i++)

{

      Console.WriteLine (arr[i]);

}

 

JM In the original submission of this class for standardization, the Clear() method was not specified as precisely as it is now. Before it was specified as just zeroing out an array. But for Booleans and reference types it needs to be false and null, respectively. The change was indeed made during the standardization process.

 

BG Array::Copy is a very interesting method, supporting all the conversions that we can prove will always work (i.e., upcasting, downcasting particular instances, boxing, unboxing, and primitive widening). Most people do not realize how useful this method can be when dealing with arrays of various primitive types.

 

------

I also wanted to highlight the fact that there is lots of good info on the CD in the back of the book. One of my favorite features is the ILASM syntax for every member. I almost cut this originally but was encouraged by two of the smartest folks I know (Don Box and Jim Miller) to include it… so I did. Here is an example, hope you find it valuable.

Array.Sort(System.Array, System.Collections.IComparer) Method

[ILASM]

.method public hidebysig static void Sort(class System.Array array, class

System.Collections.IComparer comparer)

[C#]

public static void Sort(Array array, IComparer comparer)

Summary

Sorts the elements in the specified one-dimensional System.Array using the specified

System.Collections.IComparer implementation.

----

And here is a sample application:

using System;

using System.Collections;

namespace Samples

{

      public class ArraySample

      {

            public class MyReverseComparer: IComparer

            {

                  public int Compare(Object a, Object b)

                  {

                        return -((IComparable)a).CompareTo(b);

                  }

            }

            public static void Main()

            {

                  string[] strings = {"one", "two", "three"};

                  Console.WriteLine("Array elements: ");

                  Display(strings);

                  Array.Reverse(strings);

                  Display(strings);

                  Array.Sort(strings);

                  Display(strings);

                  Array.Sort(strings, new MyReverseComparer());

                  Display(strings);

            }

            public static void Display(Array a)

            {

                  foreach(object o in a)

                        Console.Write("{0} ", o);

                  Console.WriteLine();

            }

      }

}

The output is

Array elements:

one two three

three two one

one three two

two three one

 

------

Amazon.com Sales Rank: 4,316 (up 10079 from last post)