Difference between revisions of "OOP Class12"

esse quam videri
Jump to: navigation, search
(Interfaces Part2)
Line 2: Line 2:
  
 
==Interfaces Part2==
 
==Interfaces Part2==
 +
 +
There are many interfaces in the .net framework. Some that we have been working with are the [ICollection https://msdn.microsoft.com/en-us/library/system.collections.icollection(v=vs.110).aspx] [IList https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx] and [IEnumerable https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx]
 +
 +
These interfaces allow us to use methods in a List and Array.
 +
 +
Here is an example of sorting a list of ints or an int[]
 +
<csharp>
 +
int[] ints = new int[] { 5, 4, 2, 3, 1 };
 +
            foreach (var number in ints)
 +
            {
 +
                Console.Write(string.Format("{0}\t", number));
 +
            }
 +
            Console.WriteLine();
 +
 +
            Array.Sort(ints);
 +
            foreach (var number in ints)
 +
            {
 +
                Console.Write(string.Format("{0}\t", number));
 +
            }
 +
            Console.WriteLine();
 +
           
 +
            ints = ints.Reverse().ToArray();
 +
            foreach (var number in ints)
 +
            {
 +
                Console.Write(string.Format("{0}\t", number));
 +
            }
 +
            Console.WriteLine();
 +
 +
                        //mess it up again
 +
            ints = new int[] { 5, 4, 2, 3, 1 };
 +
 +
            ints = ints.OrderBy(i => i).ToArray();
 +
            foreach (var number in ints)
 +
            {
 +
                Console.Write(string.Format("{0}\t", number));
 +
            }
 +
            Console.WriteLine();
 +
 +
            //mess it up again
 +
            ints = new int[] { 5, 4, 2, 3, 1 };
 +
 +
            //last way slightly uses agregate function to Order array by int, turns array into list so we can call foreach
 +
            ints.OrderBy(i => i).ToList().ForEach(ii=>Console.Write(string.Format("{0}\t", ii)));
 +
            Console.WriteLine();
 +
</csharp>
 +
 +
now with List
 +
<csharp>
 +
</csharp>
  
 
<!--
 
<!--

Revision as of 03:17, 24 November 2015


Interfaces Part2

There are many interfaces in the .net framework. Some that we have been working with are the [ICollection https://msdn.microsoft.com/en-us/library/system.collections.icollection(v=vs.110).aspx] [IList https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx] and [IEnumerable https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx]

These interfaces allow us to use methods in a List and Array.

Here is an example of sorting a list of ints or an int[] <csharp> int[] ints = new int[] { 5, 4, 2, 3, 1 };

           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           Array.Sort(ints);
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           
           ints = ints.Reverse().ToArray();
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
                       //mess it up again
           ints = new int[] { 5, 4, 2, 3, 1 };
           ints = ints.OrderBy(i => i).ToArray();
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           //mess it up again
           ints = new int[] { 5, 4, 2, 3, 1 };
           //last way slightly uses agregate function to Order array by int, turns array into list so we can call foreach
           ints.OrderBy(i => i).ToList().ForEach(ii=>Console.Write(string.Format("{0}\t", ii)));
           Console.WriteLine();

</csharp>

now with List <csharp> </csharp>