Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# loop over bool values

Is there a concise way to loop over true/false in C#?

I have ~20 lines of code in a unit test I'd rather not duplicate to toggle one boolean true/false.

I could break it off into a function and call it twice, but meh. This code feels more like I'm iterating over possible values than performing a distinct action with different parameters. Even if I had a function, I'd prefer the syntax of looping over the possible values rather than just calling it twice.

I could write a for loop like so...

bool toggle;
for (int i = 0; i < 2; i++)
{
    toggle = i == 1;
}

But that doesn't seem very clean.

I like this syntax:

for (bool b : { false, true }) { /* ... */ }

But it doesn't look like that will compile in C#.

Edit:

Following Jeroen's suggestion about local functions and Dmitry's answer, this is the route I went:

[TestMethod]
public void GetAndSetValue()
{
    foreach (bool toggle in new [] { false, true })
    {
        GetAndSetValue(toggle);
    }

    void GetAndSetValue(bool toggle)
    {
        // details not important
    }
}

Reasonable coders can debate whether the loop reads more easily than two function calls:

GetAndSetValue(false);
GetAndSetValue(true);

I like the loop better, so I'll roll with it until someone complains. Cheers!

like image 903
crenshaw-dev Avatar asked Jul 04 '18 14:07

crenshaw-dev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


3 Answers

Correct syntax will be foreach, not for:

foreach (bool b in new [] { false, true }) {
   /* ... */
}
like image 115
Dmitry Bychenko Avatar answered Oct 16 '22 18:10

Dmitry Bychenko


While I think simply writing a parametrized function is definitely the correct approach, the closest to that C++11 syntax that you can get in C# would be:

foreach (bool value in new [] { false, true })
{
    // ...
}
like image 41
Kwinten Avatar answered Oct 16 '22 16:10

Kwinten


I would probably just do it this way, either with a local function:

[TestMethod]
public void GetAndSetValue()
{
    GetAndSetValue(false);

    void GetAndSetValue(bool toggle)
    {
        // details not important

        if (!toggle)
            GetAndSetValue(true);
    }
}

Or "old" school with a private method.

[TestMethod]
public void GetAndSetValue()
{
    GetAndSetValue(false);
}

private void GetAndSetValue(bool toggle)
{
    // details not important

    if (!toggle)
        GetAndSetValue(true);
}
like image 1
Rand Random Avatar answered Oct 16 '22 18:10

Rand Random