Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Logical and arithmetic operations in C#

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


Written By
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 --