Translate

Tuesday, 30 May 2017

C# Array Class

Properties of Array Class:
Let me explain properties of Array Class with the help of below example:

using System;

public class Test
{
 public static void Main()
 {
     int[] arr =new int[4];
     
     for(int i=0;i<4;i++){
         arr[i]=i;
         
     }
     
     for(int i=0;i<4;i++){
         Console.Write(arr[i]+" ");
     }
     Console.WriteLine();
     //properties of array class like mentioned below
     
     //length
     Console.WriteLine(arr.Length);
     
     //LongLength
     Console.WriteLine(arr.LongLength);
     
     //Rank
     Console.WriteLine(arr.Rank);
     
     //is readonly
     Console.WriteLine(arr.IsReadOnly);
     
     //if fixed size
     Console.WriteLine(arr.IsFixedSize);

 }
}


Output:

0 1 2 3 
4
4
1
False
True 
Methods of array class

Let me explain some of the important methods with the help of below example:
using System;

public class Test
{
 public static void Main()
 {
     int[] arr =new int[4];
     
     for(int i=0;i<4;i++){
         arr[i]=i;
         
     }
     
     for(int i=0;i<4;i++){
         Console.Write(arr[i]+" ");
     }
     Console.WriteLine();
     //methods of array class like mentioned below
     
    //Reverse
    Array.Reverse(arr);
    for(int i=0;i<4;i++){
         Console.Write(arr[i]+" ");
     }
     Console.WriteLine();
     
     //Sort
    Array.Reverse(arr);
    for(int i=0;i<4;i++){
         Console.Write(arr[i]+" ");
     }
     Console.WriteLine();
     
     //GetLength() it will take argument as which dimension you want length 
     //means 0: one D
     // 1 : find the length of 2d columns
     Console.WriteLine(arr.GetLength(0));
     
     //GetType()
     Console.WriteLine(arr.GetType());

 }
}

Output:
0 1 2 3 
3 2 1 0 
0 1 2 3 
4
System.Int32[]

I hope now you have idea about array in C#. If still you have any doubt please comment below . I will try to explain with the help of example . Thanks

No comments:

Working With Java Collections