2
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..

LastTribunal
  • 5,730
  • 8
  • 34
  • 63
  • What advantage does your syntax have over `if` statements? – Blorgbeard Apr 23 '15 at 22:56
  • You need to [read this](http://stackoverflow.com/a/4966409/2589202) – crthompson Apr 23 '15 at 22:57
  • I'm really unclear of the point of this question. Is this just a rant on how you aren't happy that C# isn't a functional language? – Claies Apr 23 '15 at 23:12
  • yes, it is partially a rant. but at the same time, it asks the question of why can't C# have this language extension..| But I am happy with C#, it is what I use every day.. – LastTribunal Apr 23 '15 at 23:14

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
  • 141,979
  • 28
  • 246
  • 306
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
  • 12,744
  • 30
  • 59
  • It defers ifs to a lower level, which is an improvement – LastTribunal 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