Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Logical and arithmetic operations in C#

, 29 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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());

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Philippe Mori
Software Developer (Senior)
Canada Canada
Programmer at Maid LABS from 2003 (www.maidlabs.com)
 
Programmer-Analyst at Viasat Geo Technoligies from 1995 to 2002 (www.viasat-geo.com).
 
I have studied at École Polytechnique de Montréal in computer engineering.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by Philippe Mori
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid