Skip to content

Commit

Permalink
Added snippets for conditional logical operators (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulikov authored and Ron Petrusha committed Nov 6, 2018
1 parent aab8265 commit 11a5469
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
Expand Up @@ -26,10 +26,17 @@ private static void IntegerOperands()
private static void BooleanOperands()
{
// <SnippetBooleanOperands>
int i = 0;
bool test = false & (++i == 1);
Console.WriteLine(test); // output: False
Console.WriteLine(i); // output: 1
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool test = false & SecondOperand();
Console.WriteLine(test);
// Output:
// Second operand is evaluated.
// False
// </SnippetBooleanOperands>
}

Expand Down
@@ -0,0 +1,57 @@
using System;

namespace operators
{
public static class ConditionalLogicalOperatorsExamples
{
public static void Examples()
{
ConditionalAnd();
ConditionalOr();
}

private static void ConditionalAnd()
{
// <SnippetAnd>
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
// </SnippetAnd>
}

private static void ConditionalOr()
{
// <SnippetOr>
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = true || SecondOperand();
Console.WriteLine(a);
// Output:
// True

bool b = false || SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
// </SnippetOr>
}
}
}
6 changes: 5 additions & 1 deletion snippets/csharp/language-reference/operators/Program.cs
Expand Up @@ -22,13 +22,17 @@ static void Main(string[] args)
AssignmentExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== ?: operator examples =============");
Console.WriteLine("============== ?: operator examples ============");
ConditionalExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== ~ operator examples =============");
BitwiseComplementExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== && and || operator examples =====");
ConditionalLogicalOperatorsExamples.Examples();
Console.WriteLine();
}
}
}

0 comments on commit 11a5469

Please sign in to comment.