Click here to Skip to main content
15,890,717 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.4K   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 
The performance problem is that if you have the following operations:

bool VeryLongOperationA() { /* 1 second * / return false; }
bool VeryLongOperationB() { /* 2 seconds * / return false; }
bool VeryLongOperationC() { /* 5 seconds * / return false; }

// Standard way: Takes 1 second to complete
bool result1 =
VeryLongOperationA() &&
VeryLongOperationB() &&
VeryLongOPerationC();


// Your alternative: Takes 8 seconds to complete
bool result2 = Operation.DoSequenceOfAnd(VeryLongOperationA(),
VeryLongOperationB(),
VeryLongOperationC());

Another important problem is that often if the first check fails, subsequents check must not be done as in the follwing example:

string x = SomeFunction(); // Might return null

bool isLength4 = x != null && x.Length == 4;

If you try this with your class, then you will have:
bool isLength4WithAccessViolation =
Operation.DoSequenceOfAnd(x != null, x.Length == 4);

Thus in practice, DoSequenceOfAnd and DoSequenceOfOr are not very usable...

Again for the point of view of performance, event if all operation were fast, the code would probably be much more less efficient because of the array creation and loop...
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.