C# Arrays

Arrays are a way to manage groups of similarly typed values or objects. With arrays, you can group a series of variables and refer to them with an index. You can loop through all or part of the variables and examine or affect each in turn. You also can create arrays with multiple dimensions. Arrays in the .NET Framework have built-in functionality to facilitate many tasks. Syntax is:

datatype [ ]  NameArray;
  1.       Datatype is used to specify the type of elements to be stored in the array.
  2.    [ ] specifies the size of the array
  3.     NameArray specifies the name of the array.


Example:

 int[ ] mValue = new int[32];

 string Name = new string[4]; //The number  four is the size of the array, for  example:
 Name[0] = “Rinor Haziri”;












Is 0 because the counting starts from 0 instead of 1 . The first item indexed as 0, the next as 1 and so on.

  • Declaring and Initializing Arrays

Arrays can be declared and initialized in the same statement. When declaring an array in this manner, you must specify the type and number of the array elements. All arrays in  Visual C# are zero-based—meaning the index of the first element is zero—and numbered sequentially.When declaring an array in Visual C#, however, you must indicate the number of array elements by specifying the number of elements in the array. Thus, the upper bound of an array in Visual C# is always one less than the number used in the declaration statement. 

Example:

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Name = new string[1];
            Name[0] = "Rinor Haziri";
            foreach (string N in Name)
                Console.WriteLine("/n" + N);
            Console.ReadLine();
        }
    }

}

C# Arrays


No comments:

Post a Comment