Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the ElementAtOrDefault() LINQ extension method
By Nidhi Last Updated : November 11, 2024
VB.Net – ElementAtOrDefault() LINQ Extension Method
In this program, we will use ElementAtOrDefault() LINQ method to get the element from an array at the specified index, if the array element is not existing at the specified index then the default value will be printed on the console screen.
Program/Source Code:
The source code to demonstrate the ElementAtOrDefault() LINQ extension method is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of ElementAtOrDefault() LINQ extension method
'VB.NET program to demonstrate the
'ElementAtOrDefault() LINQ extension method.
Imports System
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim country() As String = {"India", "US", "UK", "CANADA"}
Dim val = country.ElementAtOrDefault(2)
Console.WriteLine("Element at index 2: {0}", val)
country = {}
val = country.ElementAtOrDefault(2)
Console.WriteLine("Default Value: {0}", val)
End Sub
End Module
Output
Element at index 2: UK
Default Value:
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
In the Main() function, we created an integer array that contains 7 elements. Here, we used ElementAtOrDefault() LINQ extension method to print the elements of the array at the specified index, if the array element is not existing at the specified index then the default value will be printed on the console screen.
VB.Net LINQ Query Programs »