VB.Net program to demonstrate the chain of extension methods of the 'System.Linq' namespace

Here, we are going to demonstrate the chain of extension methods of the 'System.Linq' namespace in VB.Net.
Submitted by Nidhi, on January 24, 2021 [Last updated : March 08, 2023]

VB.Net – Chain of extension methods of the System.Linq

In this program, we will use Take() and Sum() extension methods of the "System.Linq" namespace to calculate the sum of the first 4 array elements.

Program/Source Code:

The source code to demonstrate the chain of extension methods of the "System.Linq" namespace is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the chain of extension methods of the 'System.Linq' namespace

'VB.NET program to demonstrate the chain of extension methods of 
'"System.Linq" namespace.

Imports System
Imports System.IO
Imports System.Linq

Module Module1
    Sub Main()
        Dim intVals() As Integer = {50, 20, 40, 35, 10, 27, 60}

        Dim SumOfFirstFour As Double = intVals.Take(4).Sum()
        Console.WriteLine("Sum of first four array elements: {0}", SumOfFirstFour)
    End Sub
End Module

Output

Sum of first four array elements: 145
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 calculated the sum of the first 4 array elements using Take() and Sum() extension methods of the "System.Linq" namespace. After that, we printed the calculated sum on the console screen.

VB.Net LINQ Query Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.