Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Single.GetTypeCode Method in C# with Examples
Next article icon

Array.GetValue() Method in C# with Examples | Set – 1

Last Updated : 10 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows:

  • Array.GetValue(Int32, Int32)
  • Array.GetValue(Int64, Int64)
  • Array.GetValue(Int32)
  • Array.GetValue(Int64)
  • Array.GetValue(Int32, Int32, Int32)
  • Array.GetValue(Int64, Int64, Int64)
  • GetValue(Int32[])
  • GetValue(Int64[])


Here, we are explaining the first two methods i.e. Array.GetValue(Int32, Int32) and Array.GetValue(Int64, Int64) method.

GetValue(Int32, Int32) method is used to get the value at the specified position in the two-dimensional Array. The indexes are specified as 32-bit integers.

Syntax:

public object GetValue (int index1, int index2);

Here,

  • “index1” is the index of the row in which the element is located.
  • “index2” is the index of the column in which the element is located.

Return: This method returns the element at the specified index define by the passed parameters in the 2D array.

Exceptions:

  • ArgumentException: The current Array does not have exactly two dimensions.
  • IndexOutOfRangeException: Either index1 or index2 is outside the range of valid indexes for the corresponding dimension of the current Array.

GetValue(Int64, Int64) method is used to get the value at the specified position in the two-dimensional Array. The indexes are specified as 64-bit integers.

Syntax:

public object GetValue (long index1, long index2);

Here,

  • “index1” is the index of the row in which the element is located.
  • “index2” is the index of the column in which the element is located.

Return: This method returns the element at the specified index define by the passed parameters in the 2D array.

Exceptions:

  • ArgumentException: The current Array does not have exactly two dimensions.
  • IndexOutOfRangeException: Either index1 or index2 is outside the range of valid indexes for the corresponding dimension of the current Array.

Example 1:




// C# program to demonstrate Array.GetValue(Int32, Int32) 
// and array.GetValue(Int64 , Int64) method 
using System;
public class GFG {
  
public static void Main()
{
    // declare a character array 
    char[,] arr = new char[3,2]
    { 
        { 'A', 'B' },
        { 'C', 'D' },
        { 'E', 'F' } 
          
    };
      
    // using  GetValue(Int32, Int32) and 
    // GetValue(Int64, Int64) method
    Console.WriteLine("element at index [0,0] is : " + arr.GetValue(0,0));
    Console.WriteLine("element at index [1,1] is : " + arr.GetValue(1,1));
    Console.WriteLine("element at index [0,1] is : " + arr.GetValue(0,1));
    Console.WriteLine("element at index [1,0] is : " + arr.GetValue(1,0));
    Console.WriteLine("element at index [2,0] is : " + arr.GetValue(2,0));
    Console.WriteLine("element at index [2,1] is : " + arr.GetValue(2,1));
}
}
 
 

Output:

  element at index [0,0] is : A  element at index [1,1] is : D  element at index [0,1] is : B  element at index [1,0] is : C  element at index [2,0] is : E  element at index [2,1] is : F  

Example 2:




// C# program to demonstrate Array.GetValue(Int32, Int32) 
// and array.GetValue(Int64 , Int64) method 
using System;
public class GFG {
   
public static void Main()
{
    // declare a string array 
    string[,] arr = new string[3,2];
         
      // use "SetValue()" method to set 
      // the value at specified index
      arr.SetValue( "C++", 0, 0 );
      arr.SetValue( "Java", 0, 1 );
      arr.SetValue( "C#", 1, 0 );
      arr.SetValue( "Perl", 1, 1 );
      arr.SetValue( "Python", 2, 0 );
      arr.SetValue( "PHP", 2, 1 );
   
    /*the array look like
      | C++ |    | Java| 
      | C#  |    | Perl|
      | python|  | PHP |
    */
       
    // Using GetValue(Int32, Int32) and
    // GetValue(Int64, Int64) method
    Console.WriteLine("element at index [0,0] is : " + arr.GetValue(0,0));
    Console.WriteLine("element at index [1,1] is : " + arr.GetValue(1,1));
    Console.WriteLine("element at index [0,1] is : " + arr.GetValue(0,1));
    Console.WriteLine("element at index [1,0] is : " + arr.GetValue(1,0));
    Console.WriteLine("element at index [2,0] is : " + arr.GetValue(2,0));
    Console.WriteLine("element at index [2,1] is : " + arr.GetValue(2,1));
}
}
 
 

Output:

  element at index [0, 0] is : C++  element at index [1, 1] is : Perl  element at index [0, 1] is : Java  element at index [1, 0] is : C#  element at index [2, 0] is : Python  element at index [2, 1] is : PHP  

Note: For online compiler it is not possible to use 32-bit or 64-bit integer. Use offline compiler for 32 or 64-bit integer.



Next Article
Single.GetTypeCode Method in C# with Examples

S

SoumikMondal
Improve
Article Tags :
  • C#
  • Write From Home
  • CSharp-Arrays
  • CSharp-method

Similar Reads

  • Array.GetValue() Method in C# with Examples | Set – 2
    Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetVa
    3 min read
  • Array.GetValue() Method in C# with Examples | Set – 4
    Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32)Array.GetValue(Int64, Int64)Array.GetValue(Int32)Array.GetValue(Int64)Array.GetValue(
    5 min read
  • Array.GetValue() Method in C# with Examples | Set – 3
    Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32)Array.GetValue(Int64, Int64)Array.GetValue(Int32)Array.GetValue(Int64)Array.GetValue(
    5 min read
  • BitArray.RightShift() Method in C# with Examples
    BitArray class manages a array of bit values, which are represented as Booleans, where true indicates bit is 1 and false indicates bit is 0. This class is contained in namespace, System.Collections. BitArray.RightShift(Int32) method is used to shift the bits of the bit array to the right by one posi
    2 min read
  • Single.GetTypeCode Method in C# with Examples
    Single.GetTypeCode method is used to get the TypeCode for value type Single. Syntax: public TypeCode GetTypeCode (); Return Value: This method returns the enumerated constant Single. Below programs illustrate the use of the above discussed-method: Example 1: // C# program to illustrate the // Single
    1 min read
  • BitArray.LeftShift() Method in C# with Examples
    BitArray class manages a array of bit values, which are represented as Booleans, where true indicates bit is 1 and false indicates bit is 0. This class is contained in namespace, System.Collections. BitArray.LeftShift(Int32) method is used to shift the bits of the bit array to the left by one positi
    2 min read
  • SByte.Equals Method in C# with Examples
    SByte.Equals Method is used to get a value which indicates whether the current instance is equal to a specified object or SByte or not. There are 2 methods in the overload list of this method as follows: Equals(SByte) Method Equals(Object) Method SByte.Equals(SByte) Method This method is used to ret
    3 min read
  • Array.BinarySearch(Array, Object) Method with examples in C#
    This method is used to search a specific element in the entire one-dimensional sorted array by using the IComparable interface which is implemented by each element of the array and by the specified object. Syntax: public static int BinarySearch (Array array, object value); Parameters: array: It is t
    4 min read
  • Single.Equals() Method in C# with Examples
    Single.Equals() Method is used to get a value which indicates whether the two instances of Single represents the same value or not. There are 2 methods in the overload list of this method as follows: Equals(Single) Method Equals(Object) Method Single.Equals(Single) Method This method is used to retu
    3 min read
  • SByte.ToString Method in C# with Examples | Set - 2
    SByte.ToString Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows: ToString(IFormatProvider) Method ToString(String, IFormatProvider) Method ToString() Method ToString(String)
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences