Home » .Net

Explain Length and GetLength() in C#

Learn: What is the difference between Length and GetLength() in C#, when and where they are used in C# program?

In C#.Net, Length and GetLength() are basically used with the arrays, most of the times these two things are confusing for the developers. In this post we are going to learn what is the difference between Length and GetLength() in C#? When and where they should be used?

Length is a property which specifies the total number of elements in array. While GetLength() is a pre-define method of Array class. It has an argument which specify the dimension. If we pass 0 to GetLenth() method then it returns the size of first dimension. And if we pass 1 to GetLegnth() method then it returns the size of second dimension.

Example of Length Property

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int[] X;

            X = new int[5];

            Console.Write("Enter Elements : \n");
            for (i = 0; i < X.Length; i++)
            {
                Console.Write("\tElement[" + i + "]: ");
                X[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Write("\n\nElements are: \n");
            for (i = 0; i < X.Length; i++)
            {
                Console.WriteLine("\tElement[" + i + "]: "+X[i]);
            }          
        }
    }
}

Output

Enter Elements :      
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50
		
Elements are:         
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50 
Press any key to continue . . .

In the statement for (i = 0; i < X.Length; i++), we used X.Length property which is returning length of the array that is 5.

Example of GetLength() Method

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;
            int[,] X;

            X = new int[2,3];

            Console.Write("Enter Elements : \n");
            for (i = 0; i < X.GetLength(0); i++)
            {
                for (j = 0; j < X.GetLength(1); j++)
                {
                    Console.Write("\tElement[" + i + ","+j+"]: ");
                    X[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.Write("\n\nElements are: \n");
            for (i = 0; i < X.GetLength(0); i++)
            {
                for (j = 0; j < X.GetLength(1); j++)
                {
                    Console.Write(X[i, j] + " ");
                }
                Console.WriteLine();
            }

        }
    }
}

Output

Enter Elements :        
        Element[0,0]: 11
        Element[0,1]: 22
        Element[0,2]: 33
        Element[1,0]: 44
        Element[1,1]: 55
        Element[1,2]: 66
                        
                        
Elements are:           
11 22 33                
44 55 66
Press any key to continue . . .

In above example GetLength(0) specify number of rows and GetLength(1) specify number of columns.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.