Logical and arithmetic operations in C#






4.71/5 (4 votes)
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
{
T additionResult = default(T);
Func<T, T, T> adder = ExpressionGenerator.AdditionExpression<T>();
Array.ForEach(sequenceOfBooleanValue,
(item) =>
{
additionResult = adder(item, additionResult);
});
return additionResult;
}
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");
BinaryExpression body = Expression.Add(parameterA, parameterB);
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();
}
}
To browse or download the source code, please visit here[^].