Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the Sum() extension method
By Nidhi Last Updated : November 13, 2024
VB.Net – Sum() Extension Method
In this program, we will use the Sum() extension method of the "System.Linq" namespace to calculate the sum of array elements.
Program/Source Code:
The source code to demonstrate the Sum() extension method is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of Sum() extension method
'VB.NET program to demonstrate the
'Sum() extension method.
Imports System
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim intVals() As Integer = {50, 20, 40, 35, 10, 27, 60}
Dim s As Double = intVals.Sum()
Console.WriteLine("Sum of array elements: {0}", s)
End Sub
End Module
Output
Sum of array elements: 242
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 array elements using the Sum() extension method of the "System.Linq" namespace. After that, we printed the calculated sum on the console screen.
VB.Net LINQ Query Programs »