Click here to Skip to main content
15,038,345 members
Articles / Programming Languages / C#
Alternative
Tip/Trick
Posted 29 Jun 2011

Tagged as

Stats

5.6K views

Logical and arithmetic operations in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 Jun 2011CPOL
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:


C#
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:


C#
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 --