Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Describe your changes here.

Fixes dotnet/docs#Issue_Number (if available)
Fixes dotnet/docs#Issue_Number or dotnet/dotnet-api-docs#Issue_Number (if available)
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Example
{
Console::WriteLine( "The two addresses are not equal" );
}
// Will output "The two addresses are equal"
//</snippet8>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static void SampleEquals()
Console.WriteLine("The two addresses are equal");
else
Console.WriteLine("The two addresses are not equal");
// Will output "The two addresses are equal"
//</snippet8>
}

Expand Down
165 changes: 165 additions & 0 deletions snippets/csharp/language-reference/operators/LogicalOperators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System;

namespace operators
{
public static class LogicalOperators
{
public static void Examples()
{
Console.WriteLine("==== ! operator");
Negation();

Console.WriteLine("==== &, |, ^ operators");
And();
Or();
Xor();

Console.WriteLine("==== && and || operators");
ConditionalAnd();
ConditionalOr();

Console.WriteLine("==== Compound assignment and precedence");
CompoundAssignment();
Precedence();
}

private static void Negation()
{
// <SnippetNegation>
bool passed = false;
Console.WriteLine(!passed); // output: True
Console.WriteLine(!true); // output: False
// </SnippetNegation>
}

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

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

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

bool test = true | SecondOperand();
Console.WriteLine(test);
// Output:
// Second operand is evaluated.
// True
// </SnippetOr>
}

private static void Xor()
{
// <SnippetXor>
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False
// </SnippetXor>
}

private static void ConditionalAnd()
{
// <SnippetConditionalAnd>
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
// </SnippetConditionalAnd>
}

private static void ConditionalOr()
{
// <SnippetConditionalOr>
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
// </SnippetConditionalOr>
}

private static void CompoundAssignment()
{
// <SnippetCompoundAssignment>
bool test = true;
test &= false;
Console.WriteLine(test); // output: False

test |= true;
Console.WriteLine(test); // output: True

test ^= false;
Console.WriteLine(test); // output: True
// </SnippetCompoundAssignment>
}

private static void Precedence()
{
// <SnippetPrecedence>
Console.WriteLine(true | true & false); // output: True
Console.WriteLine((true | true) & false); // output: False

bool Operand(string name, bool value)
{
Console.WriteLine($"Operand {name} is evaluated.");
return value;
}

var byDefaultPrecedence = Operand("A", true) || Operand("B", true) && Operand("C", false);
Console.WriteLine(byDefaultPrecedence);
// Output:
// Operand A is evaluated.
// True

var changedOrder = (Operand("A", true) || Operand("B", true)) && Operand("C", false);
Console.WriteLine(changedOrder);
// Output:
// Operand A is evaluated.
// Operand C is evaluated.
// False
// </SnippetPrecedence>
}
}
}
20 changes: 8 additions & 12 deletions snippets/csharp/language-reference/operators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ static void Main(string[] args)
ArithmeticOperators.Examples();
Console.WriteLine();

Console.WriteLine("============== == and != operator examples =====");
EqualityAndNonEqualityExamples.Examples();
Console.WriteLine();

Console.WriteLine("======== Logical operators examples ============");
LogicalOperators.Examples();
Console.WriteLine();

Console.WriteLine("============== + operator examples =============");
AdditionExamples.Examples();
Console.WriteLine();
Expand All @@ -30,14 +38,6 @@ static void Main(string[] args)
BitwiseComplementExamples.Examples();
Console.WriteLine();

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

Console.WriteLine("============== == and != operator examples =====");
EqualityAndNonEqualityExamples.Examples();
Console.WriteLine();

Console.WriteLine("======= >, <, >=, and <= operator examples =====");
GreaterAndLessOperatorsExamples.Examples();
Console.WriteLine();
Expand All @@ -58,10 +58,6 @@ static void Main(string[] args)
ShiftOperatorsExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== ! operator examples =============");
LogicalNegationExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== . operator examples =============");
MemberAccessExamples.Examples();
Console.WriteLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Public Class Test
Else
Console.WriteLine("The two addresses are not equal")
End If
' Will output "The two addresses are equal"
'</snippet8>
End Sub 'SampleEquals

Expand Down