Click here to Skip to main content
15,881,882 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 9.3K   2
The code has been updated to improve the performance:public static bool DoSequenceOfOr(params Func[] sequenceOfBooleanValue){ foreach (Func item in sequenceOfBooleanValue) if (item()) return true; return false;}public static bool...
The code has been updated to improve the performance:
C#
public static bool DoSequenceOfOr(params Func<bool>[] sequenceOfBooleanValue)
{
    foreach (Func<bool> item in sequenceOfBooleanValue)
        if (item())
            return true;
    return false;
}

public static bool DoSequenceOfAnd(params Func<bool>[] sequenceOfBooleanValue)
{
    foreach (Func<bool> item in sequenceOfBooleanValue)
        if (!item())
            return default(bool);
    return true;
}


Usage:

bool result1 = Operation.DoSequenceOfAnd(MethodOne, MethodTwo, MethodThree)
bool result2 = Operation.DoSequenceOfOr(MethodOne, MethodTwo, MethodThree)

License

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


Written By
Software Developer
Australia Australia

Comments and Discussions

 
GeneralThe performance problem is that if you have the following op... Pin
Philippe Mori28-Jun-11 14:51
Philippe Mori28-Jun-11 14:51 
GeneralRe: Well, I haven't noticed that functions were used in the alte... Pin
Philippe Mori28-Jun-11 15:12
Philippe Mori28-Jun-11 15:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.