rorschack

C# ArrayList example

Jul 17th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. // Author: Yash D. Saraf <https://github.com/yashdsaraf>
  2. using System;
  3. using System.Collections;
  4.  
  5. class Sample  {
  6.  
  7.     static void Main()  {
  8.  
  9.         ArrayList list = new ArrayList();
  10. // Add elements to list using ArrayList.Add( string element ) method
  11.         list.Add( "Mercury" );
  12.         list.Add( "Venus" );
  13.         list.Add( "Earth" );
  14.         list.Add( "Mars" );
  15.         list.Add( "Jupiter" );
  16.         list.Add( "Saturn" );
  17.         list.Add( "Uranus" );
  18.         list.Add( "Neptune" );
  19. // Get capacity of list using ArrayList.Capacity property
  20.         Console.WriteLine( "Size: {0}", list.Capacity );
  21. // Get number of elements in list using ArrayList.Count property
  22.         Console.WriteLine( "No. of elements: {0}", list.Count );
  23.         Console.WriteLine( "Elements: " );
  24.         PrintVals( list );
  25.         Console.WriteLine( "Elements sorted in ascending order: " );
  26. // Sort elements in list using ArrayList.Sort() method
  27.         list.Sort();
  28.         PrintVals( list );
  29.         Console.WriteLine( "Elements sorted in descending order: " );
  30. // Reverse elements in list using ArrayList.Reverse() method
  31.         list.Reverse();
  32.         PrintVals( list );
  33.         Console.ReadKey();
  34.     }
  35.  
  36.     private static void PrintVals( ArrayList list )  {
  37.         foreach ( string s in list )
  38.             Console.WriteLine( "\t{0}", s );
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment