1
if(someBoolTest()) dothis()
else dothat();

or just

if(someBoolTest()) dothis();

Wouldn't it be nice to do something like:

someBoolTest() => {dothis(),dothat()}

or

someBoolTest() => dothis()

Is this done in other languages? How do we do this in C#? (I don't think we can, so then why not?)

EDIT: I am aware of ternary ops, but that doesn't make it look any better. Would be nice to do this with some form of lambda with delegates..

I Stand With Russia
  • 6,254
  • 8
  • 39
  • 67

4 Answers4

4

How do we do this in C#?

Using the if-else clause, just as you did in your example.

You could be creative and create something of this sort:

(SomeBoolTest() ? (Action)DoThis : DoThat)();

But that is terribly unreadable code, don't do that.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

You can, but should not, do something that resembles the syntax that you mentioned, by doing something stupid like writing an extension method over a bool type, as shown in the below example:

public static class UselessExtensions
{
    public static void WhenTrue(this bool evaluatedPredicate, Action whenTrue)
    {
        if (evaluatedPredicate)
            whenTrue();
    }
}

public static class TryingUselessExtensions
{
    public static bool SomeBoolTest()
    {
        return true;
    }

    public static void DoIt()
    {
        SomeBoolTest().WhenTrue(() => Console.WriteLine(true));
    }
}
Alex
  • 13,024
  • 33
  • 62
  • It defers ifs to a lower level, which is an improvement – I Stand With Russia Apr 24 '15 at 00:18
  • @Claies As you can see, the answer specifically states "something that resembles the **syntax** ...", its purpose is not to get rid of the if statement. – Alex Apr 24 '15 at 00:19
  • 1
    true, but in other comments the poster has suggested the reason for the question in the first place is their dislike of `if-else`, referring to it as "these dirty words, will be removed from all evolved languages". It isn't a sensible statement, but the poster has made it clear they want an alternative to `if-else`, not just syntax to hide it (unless I'm way off). – Claies Apr 24 '15 at 00:25
0

I believe ternary operators are what you're looking for:

variable = condition ? value_if_true : value_if_false

So, for example you want an int to equal 0 if your condition is met, and 3 if it is not:

int this = 500;
int that = 700;
int n = (this==that) ? 0 : 3;

In this case n would be assigned the value of 3! There's a good wikipedia page on this, head on over and give it a look :)

Wikipedia page on ternary operators

  • This approach is useful when you want a conditional expression to return a value and each operand is a function that returns a value. However, the question is about how to conditionally execute a pair of functions where neither returns a value. – DavidRR Sep 29 '15 at 14:06
-1

You do it like this

    bool myBool = true;
    bool newBool;
    public void Main()
    {
        MyFunction( newBool = (aFunctionThatReturnsABool == true) ? true: false);
    }

    public void MyFunction (bool aBool)
    {
        // stuff based on the bool
    }

But what are you actually trying to do?

Winky2222
  • 46
  • 7