Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Logical and arithmetic operations in C#

, 29 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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:
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)

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid