C Sharp Arrays and Strings




Array is a storage location of fixed sized elements of the same datatype. in other terms, Array stores the collection of variables or collection of values in a memory. We can combine the number of variables to be declared in the array other than declaring seperately. The elements in the array can be accessed using indexes. An Array is collection variables of the same data type. It is better to say variables instead of values.An Array stores the variables of the same Data Type in Contiguous Memory Locations.


  • Declaration Of Array


  • Syntax

    data_type [] array_name;

    Example

    int [] a;

    where

    int- data_type

    []- size of array

    a- name of the array



  • Initialization Of Array


  • int [] a =new int[10];

    This allocates memory to store 10 integer elements.



  • Assigning value to Array Elements


  • You can assign value to each element of array seperately.

    Example

    int [] a =new int [10];

    a[0]=10;

    Note:Array index starts from zero.

    Assigning values at time of declaration

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

    Also in another way

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

    Also omiting size


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

    Program



    using System;

    namespace array

    {

    class Program

    {
         static void Main(string[] args)
         {

              int[] myarray = new int[10];

              int i,j;

              for (i = 1; i <= 5; i++)
             {

                   myarray[i] = i + 10;

              }
              for (j = 1; j <= 5; j++)
             {

                  Console.WriteLine("Element {0}", j);

             }

             Console.ReadKey();

          }

     }

    }
    Output

    Element 1
    Element 2
    Element 3
    Element 4
    Element 5
  • Multidimensional Array
  • C# allows us to declare Multidimensional Arrays. The syntax to declare Multidimensional Arrays is slightly different than Multidimensional Arrays declared in other Programming languages. Multidimensional Arrays are also called as Rectangular Arrays. Declaration of Multidimensional Arrays.

    Two Dimensional Array
    int [,] k;


    Three Dimensional Array
    string [,,] str;


  • Two Dimensional Array


  • The Two Dimensional Array is a form table having Rows and Columns.



    Above is the Two Dimensional Array with 3 Rows and Columns. 'K' is the name of the Array and the values in brackets are subscripts/indexes. We can access the elements in the Array with the subscripts k[i,j] where I refers to Row and J refer to Column.
  • Initialization Two Dimensional Array


  • The Two Dimensional Array can be initialized as follows.
    int [,] k=new int [3,3]={{0,0,0},{0,2,3},{2,4,2}};

    Program
    using System;
    namespace multidimensional_Array
    {
            class Program
                  {
                         static void Main(string[] args)
                        {
                                  int[,] k = new int[3, 3]{{2,2,4},{3,2,4},{4,3,2}};
                                  int i, j; 
                                  Console.WriteLine("The values at 2nd row and 3rd column is a[{0},{1}]:{2}", 2,3,k[1,2]);
                                  for (i = 0; i < 3; i++)
                                 {
                                         for (j = 0; j < 3; j++) 
                                        { 
                                                Console.WriteLine("a[{0},{1}] = {2}", i, j, k[i, j]);
                                        }
                                 } Console.ReadLine();
                        }
                }
    }


    Output

    The values at 2nd row and 3rd column is a[{0},{1}]:{2}= a[2,3]:4
    a[0,0] = 2
    a[0,1] = 2
    a[0,2] = 4
    a[1,0] = 3
    a[1,1] = 2
    a[1,2] = 4
    a[2,0] = 4
    a[2,1] = 3
    a[2,2] = 2

    Strings

    In C Sharp , Strings are array of characters or we can directly declare a string variable using String Data type.

    Example.

    string name="Raj";

    Also string can be made by joining two strings

    string car="Audi";

    string color="black";

    string vehicle=car+color;

    output.
    Audi black.


    char [] vehicle={'A', 'U', 'D', 'I'};

    string car=new string(vehicle);

    Output.
    Audi


    using System; namespace strings
    {
         class Program
         {

             static void Main(string[] args)

             {

                  string s1 = "Jaguar";

                  string s2 = "car";

                  int res = 0;

                //Compare two strings

                //1 - Not equal

                //0 - equal

                  res = String.Compare(s1,s2);

                  Console.WriteLine("Result of comparison is " +res);


               //check if specified string value is present in String

               //True- String contains specified letter

               //False-String does contains specified letter


                 bool b = s1.Contains('a');

                 Console.WriteLine(b);


               //create copy of string

                 string copy = String.Copy(s1);

                 Console.WriteLine(copy);


                //check whether the strings are equal

                //True- equal

                 //False-Not equal

                   bool e = copy.Equals(s1);

                   Console.WriteLine(e);


                //return Zero based first occurance of charater specified


                  int ch=s1.IndexOf('g');

                  Console.WriteLine(ch);

                //check whether the string is null or empty

                //True- Yes empty or null string

                 //False- No Not empty or Null string


                  bool s = String.IsNullOrEmpty(s1);

                  Console.WriteLine(s);

                  Console.ReadKey();

             }
           }
      }


    Output.

    Result of comparison is 1
    True
    jaguar
    True
    2
    False
    C Sharp Arrays and Strings C Sharp Arrays and Strings Reviewed by LanguageExpert on August 09, 2017 Rating: 5

    No comments