12,831,378 members (30,480 online)
alternative version

#### Stats

30.1K views
8 bookmarked
Posted 26 Jun 2011

# Logical and arithmetic operations in C#

, 26 Jun 2011 CPOL
 Rate this:
Some logical and arithmetic operations in C#

There are many uses of logical and arithmetic operators in C#. And also the usage of these operators are different
depending on the situation. For example, here is a simple use of the `&&` operator:

`bool booleanResult = MethodReturnBooleanOne() && MethodReturnBooleanTwo() && MethodReturnBooleanThree();`

and a simple use of the `+` operator:

`int result = MethodIntReturn1() + MethodIntReturn2() + MethodIntReturn3() + MethodIntReturn4();`

We could delegate this logical operation( `&&` or `||`) on a specific method and let the method
do the operation and pass back the result to the caller. Also, it is possible to do the same for an arithmetic operation, for example,
`3+5+5+5` could be done by just calling a method and passing 3,5,5,5 to that method, and the method will do the job and return back
the result to the caller. To describe the whole point, I created a few methods; for example:

`DoSequenceOfAnd(params bool[] sequenceOfBooleanValue)`

This method will take the values of `boolean `and will do an `&&` (AND) operation on those values and return the result to the caller.

`DoSequenceOfOr(params bool[] sequenceOfBooleanValue)`

This method will take the values of `boolean `and will do an `||` (OR) operation on those values and return the result to the caller.

`DoSequenceOfXOr(params bool[] sequenceOfBooleanValue)`

This method will take the values of `boolean `and do an `^` (XOR) operation on those values and return the result to the caller.

`DoSequenceOfPlus<T>(params T[] sequenceOfTValues)`

This generic method will accept a sequence of values (in here, `T` should only define numeric types from the value types)
and will do an addition operation and return the result.

`DoSequenceOfMultiply<T>(params T[] sequenceOfTValues)`

This generic method will accept a sequence of values (in here, `T` should only define numeric types from the value types)
and will do a multiplication operation and return the result.

Here is the code for the above methods:

```public static class Operation
{
public static bool DoSequenceOfAnd(params bool[] sequenceOfBooleanValue)
{
bool andResult = true;
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
andResult &= item;
});
return andResult;
}
public static bool DoSequenceOfOr(params bool[] sequenceOfBooleanValue)
{
bool orredResult = default(bool);
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
orredResult |= item;
});
return orredResult;
}
public static bool DoSequenceOfXOr(params bool[] sequenceOfBooleanValue)
{
bool xoredResult = default(bool);
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
xoredResult ^= item;
});
return xoredResult;
}
public static T DoSequenceOfPlus<T>(params T[] sequenceOfTValues)
where T : struct
{
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
});
}
public static T DoSequenceOfMultiply<T>(params T[] sequenceOfTValues)
where T : struct
{
dynamic multiplicationResult = (DoSequenceOfPlus<int>(default(int), 1));
Func<T, T, T> multiplier = ExpressionGenerator.MultiplicationExpression<T>();
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
multiplicationResult = multiplier(item, multiplicationResult);
});
return multiplicationResult;
}
}```

Here is the usage of the above methods:

```static void Main(string[] args)
{
bool booleanResult = MethodOne() && MethodTwo() && MethodThree();
bool resultAnd = Operation.DoSequenceOfAnd(MethodOne(), MethodTwo(), MethodThree());
bool resultOr = Operation.DoSequenceOfOr(MethodOne(), MethodTwo(), MethodThree());
bool resultXOr = Operation.DoSequenceOfXOr(MethodOne(), MethodTwo(), MethodThree());
int resultOfIntAddition = Operation.DoSequenceOfPlus<int>(3, 3, 5, 5);
int resultOfIntMultiplication = Operation.DoSequenceOfMultiply<int>(3, 3, 3, 1);
long resultOfLongMultiplication = Operation.DoSequenceOfMultiply<long>(1, 3, 1, 1);
}```

and related code used by the `DoSequenceOfPlus `and `DoSequenceOfMultiply ` methods:

```public static class ExpressionGenerator
{
public static Func<T, T, T> AdditionExpression<T>()
{
ParameterExpression
parameterA = Expression.Parameter(typeof(T), "a"),
parameterB = Expression.Parameter(typeof(T), "b");
return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
}
public static Func<T, T, T> MultiplicationExpression<T>()
{
ParameterExpression
parameterA = Expression.Parameter(typeof(T), "a"),
parameterB = Expression.Parameter(typeof(T), "b");
BinaryExpression body = Expression.Multiply(parameterA, parameterB);
return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
}
}```

## Share

 Software Developer Australia
Designer and Architect.
Author of the

## You may also be interested in...

 Pro

 First Prev Next
 You have worked hard for this, showed good examples but I ag... All Time Programming4-Jul-11 21:41 All Time Programming 4-Jul-11 21:41
 Another problem with your solution is that every time a new ... Philippe Mori28-Jun-11 16:44 Philippe Mori 28-Jun-11 16:44
 Reason for my vote of 4 The question in itself is not so bad... Philippe Mori28-Jun-11 16:32 Philippe Mori 28-Jun-11 16:32
 It would be interesting to see the performance comparison of... Philippe Mori28-Jun-11 16:07 Philippe Mori 28-Jun-11 16:07
 Tried to separate the logical operation code into a dedicate... Mohammad A Rahman28-Jun-11 3:08 Mohammad A Rahman 28-Jun-11 3:08
 I don't think I understand what the problem is that you are ... tallies28-Jun-11 2:36 tallies 28-Jun-11 2:36
 This might be good for academic purpose but not of any pract... RakeshMeena26-Jun-11 20:22 RakeshMeena 26-Jun-11 20:22
 Reason for my vote of 1 This seems like a pretty bad way of ... EdMan19626-Jun-11 1:33 EdMan196 26-Jun-11 1:33
 Last Visit: 31-Dec-99 19:00     Last Update: 30-Mar-17 14:05 Refresh 1