Skip to content

Commit cf2902c

Browse files
pkulikovBillWagner
authored andcommitted
Additional snippets for the logical operators overview (#797)
1 parent 6ca0b78 commit cf2902c

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

snippets/csharp/language-reference/operators/LogicalOperators.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ public static class LogicalOperators
66
{
77
public static void Examples()
88
{
9-
Console.WriteLine("==== ! operator");
9+
Console.WriteLine("==== !, &, |, ^ operators");
1010
Negation();
11-
12-
Console.WriteLine("==== &, |, ^ operators");
1311
And();
1412
Or();
1513
Xor();
14+
WithNullableBoolean();
1615

1716
Console.WriteLine("==== && and || operators");
1817
ConditionalAnd();
@@ -41,11 +40,17 @@ bool SecondOperand()
4140
return true;
4241
}
4342

44-
bool test = false & SecondOperand();
45-
Console.WriteLine(test);
43+
bool a = false & SecondOperand();
44+
Console.WriteLine(a);
4645
// Output:
4746
// Second operand is evaluated.
4847
// False
48+
49+
bool b = true & SecondOperand();
50+
Console.WriteLine(b);
51+
// Output:
52+
// Second operand is evaluated.
53+
// True
4954
// </SnippetAnd>
5055
}
5156

@@ -55,11 +60,17 @@ private static void Or()
5560
bool SecondOperand()
5661
{
5762
Console.WriteLine("Second operand is evaluated.");
58-
return false;
63+
return true;
5964
}
6065

61-
bool test = true | SecondOperand();
62-
Console.WriteLine(test);
66+
bool a = true | SecondOperand();
67+
Console.WriteLine(a);
68+
// Output:
69+
// Second operand is evaluated.
70+
// True
71+
72+
bool b = false | SecondOperand();
73+
Console.WriteLine(b);
6374
// Output:
6475
// Second operand is evaluated.
6576
// True
@@ -120,6 +131,19 @@ bool SecondOperand()
120131
// </SnippetConditionalOr>
121132
}
122133

134+
private static void WithNullableBoolean()
135+
{
136+
// <SnippetWithNullableBoolean>
137+
bool? test = null;
138+
Display(!test); // output: null
139+
Display(test ^ false); // output: null
140+
Display(test ^ null); // output: null
141+
Display(true ^ null); // output: null
142+
143+
void Display(bool? b) => Console.WriteLine(b is null ? "null" : b.Value.ToString());
144+
// </SnippetWithNullableBoolean>
145+
}
146+
123147
private static void CompoundAssignment()
124148
{
125149
// <SnippetCompoundAssignment>

0 commit comments

Comments
 (0)