Array.GetValue() Method in C# with Examples | Set – 2
Last Updated :
10 May, 2020
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
Array.GetValue(Int32) and
Array.GetValue(Int64) method.
GetValue(Int32) method is used to get the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Syntax:
public object GetValue (int index);
Here, the
index is the 32-bit integer that represents the position of the Array element to get.
Returns: This method returns the value at the specified position in the one-dimensional Array of object type.
Exceptions:
- ArgumentException: If the current Array does not have exactly one dimension.
- IndexOutOfRangeException: If the index is outside the range of valid indexes for the current Array.
GetValue(Int64) method is used to get the value at the specified position in the one-dimensional Array. The index is specified as a 64-bit integer.
Syntax:
public object GetValue (long index);
Here, the
index is the 64-bit integer that represents the position of the Array element to get.
Returns: This method returns the value at the specified position in the one-dimensional Array of object type.
Exceptions:
- ArgumentException: If the current Array does not have exactly one dimension.
- IndexOutOfRangeException: If the index is outside the range of valid indexes for the current Array.
Example 1:
csharp
// C# program to demonstrate the
// Array.GetValue(Int32) Method
using System;
public class GFG {
public static void Main()
{
// declare a character array
char[] arr = new char[]{'A', 'B', 'C', 'D'};
// use of GetValue(Int32) Method
Console.WriteLine("element at index 3 is : " + arr.GetValue(3));
Console.WriteLine("element at index 1 is : " + arr.GetValue(1));
Console.WriteLine("element at index 2 is : " + arr.GetValue(2));
Console.WriteLine("element at index 0 is : " + arr.GetValue(0));
}
}
Output:
element at index 3 is : D
element at index 1 is : B
element at index 2 is : C
element at index 0 is : A
Example 2:
csharp
// C# program to demonstrate the
// Array.GetValue(Int64) Method
using System;
public class GFG {
public static void Main()
{
// declare a string array
string[] arr = new string[5];
// use "SetValue()" method to set
// the value at specified index
arr.SetValue( "C++", 0 );
arr.SetValue( "Java", 1 );
arr.SetValue( "C#", 2 );
arr.SetValue( "Perl", 3 );
arr.SetValue( "Python", 4 );
// Using GetValue(Int32) Method
Console.WriteLine("element at index 3 is : " + arr.GetValue(3));
Console.WriteLine("element at index 1 is : " + arr.GetValue(1));
Console.WriteLine("element at index 2 is : " + arr.GetValue(2));
Console.WriteLine("element at index 0 is : " + arr.GetValue(0));
Console.WriteLine("element at index 0 is : " + arr.GetValue(4));
}
}
Output:
element at index 3 is : Perl
element at index 1 is : Java
element at index 2 is : C#
element at index 0 is : C++
element at index 0 is : Python
Note: For online compiler it's not possible to use 32-bit or 64-bit integer. Use offline compiler for 32 or 64-bit integer.
Similar Reads
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 - 1 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
4 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
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
Array.LastIndexOf Method in C# | Set â 2 Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the over
4 min read
Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C# Array.BinarySearch(Array, int32, int32, Object) Method is used to search a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. It searches only in a specified boundary that the user defi
4 min read