65.9K
CodeProject is changing. Read more.
Home

Logical and arithmetic operations in C#

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jun 29, 2011

CPOL
viewsIcon

6500

Instead of doing it yourself, a better way would be to use LINQ for that purpose. Here is an example for addition:int[] numbers = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };int result = numbers.Aggregate((x, y) => x + y).ToString());and for multiplication:long[] numbers = { 4, 5, 6, 7,...

Instead of doing it yourself, a better way would be to use LINQ for that purpose. Here is an example for addition:

int[] numbers = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int result = numbers.Aggregate((x, y) => x + y).ToString());

and for multiplication:

long[] numbers = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
long result = numbers.Aggregate(1L, (x, y) => x * y).ToString());
Logical and arithmetic operations in C# - CodeProject