Beispiel #1
0
        static void Main()
        {
            int[] array = new int[] { 3, 2, 1, 4, 0, 5 };
            Program2.PrintArray(array);

            //green arrow start from the beginning to the 2nd last element
            for (int green = 0; green < array.Length - 1; green++)
            {
                //red arrow start from one element after green arrow
                //and ends at the last element
                //last element : (red < array.Length) OR (red <= array.Length - 1)
                for (int red = green + 1; red < array.Length; red++)
                {
                    //compare
                    //if values pointed by red arrow is smaller than values
                    //pointed by green arrow, we swap them
                    if (array[red] > array[green])
                    {
                        //swap
                        int temp = array[red];
                        array[red]   = array[green];
                        array[green] = temp;
                    }
                }
            }
            Program2.PrintArray(array);
        }
Beispiel #2
0
 //Passing Array(ref type variable) by Value
 static void Main()
 {
     //A is an array, reference type
     //A contains the reference to an array somewhere else in memory
     int[] A = new int[] { 1, 2, 3 };
     Program2.PrintArray(A); //1,2,3
     Increment(A);
     Program2.PrintArray(A); //1,2,3 or 2,3,4?
 }
Beispiel #3
0
        static void Main()
        {
            int[] A = new int[] { 1, 2, 3 };
            Program2.PrintArray(A);

            //we want all element of A to be (+1)
            ApplyOperation(A, Increment);
            Program2.PrintArray(A);

            //we want all element of A to be (*5)
            IntegerOps multiply5 = delegate(int y)
            {
                return(y * 5);
            };

            ApplyOperation(A, multiply5);
            Program2.PrintArray(A);

            //we want all element of A to be raised to the power of 2
            ApplyOperation(A, (int w) =>
                           { return(w * w); });
            Program2.PrintArray(A);
        }