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
4 changes: 4 additions & 0 deletions snippets/csharp/keywords/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ static void Main(string[] args)
IterationKeywordsExamples.Examples();
Console.WriteLine("================= readonly Keyword Examples ======================");
ReadonlyKeywordExamples.Examples();
Console.WriteLine("================= true and false operators examples ==============");
LaunchStatusTest.Main();
Console.WriteLine("================= true and false literals examples ===============");
TrueFalseLiteralsExamples.Examples();
}
}
}
33 changes: 33 additions & 0 deletions snippets/csharp/keywords/TrueFalseLiteralsExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace keywords
{
public static class TrueFalseLiteralsExamples
{
public static void Examples()
{
TrueExample();
FalseExample();
}

private static void TrueExample()
{
// <SnippetTrueLiteral>
bool check = true;
Console.WriteLine(check ? "Passed" : "Not passed");
// Output:
// Passed
// </SnippetTrueLiteral>
}

private static void FalseExample()
{
// <SnippetFalseLiteral>
bool check = false;
Console.WriteLine(check ? "Passed" : "Not passed");
// Output:
// Not passed
// </SnippetFalseLiteral>
}
}
}
60 changes: 60 additions & 0 deletions snippets/csharp/keywords/TrueFalseOperatorsExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

public struct LaunchStatus
{
public static readonly LaunchStatus Green = new LaunchStatus(0);
public static readonly LaunchStatus Yellow = new LaunchStatus(1);
public static readonly LaunchStatus Red = new LaunchStatus(2);

private int status;

private LaunchStatus(int status)
{
this.status = status;
}

public static bool operator true(LaunchStatus x) => x == Green || x == Yellow;
public static bool operator false(LaunchStatus x) => x == Red;

public static LaunchStatus operator &(LaunchStatus x, LaunchStatus y)
{
if (x == Red || y == Red || (x == Yellow && y == Yellow))
{
return Red;
}

if (x == Yellow || y == Yellow)
{
return Yellow;
}

return Green;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's an issue here. For x & y & z work correctly when z is Yellow, this should return Yellow when one of x or y is Yellow.

Do you agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner

For x & y & z work correctly...

to clarify: by "correctly" you mean that the & overload must be associative, so (x & y) & z == x & (y & z)

Yes, I agree then. Will make an update soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner I've addressed your feedback; thanks for review.

I've checked the updated definition for all possible x, y, and z values: now the & operator is commutative and associative. In particular, that means that x1 & ... & xN is Yellow iff there is only one operand which is Yellow with all others being Green. That's why I've updated the true definition to return true value for Yellow operand. The following requirement is met:

if only one sub-system is yellow, we can launch.

}

public static bool operator ==(LaunchStatus x, LaunchStatus y) => x.status == y.status;
public static bool operator !=(LaunchStatus x, LaunchStatus y) => !(x == y);

public override bool Equals(object obj) => obj is LaunchStatus other && this == other;
public override int GetHashCode() => status;
}

public class LaunchStatusTest
{
public static void Main()
{
LaunchStatus okToLaunch = GetFuelLaunchStatus() && GetNavigationLaunchStatus();
Console.WriteLine(okToLaunch ? "Ready to go!" : "Wait!");
}

static LaunchStatus GetFuelLaunchStatus()
{
Console.WriteLine("Getting fuel launch status...");
return LaunchStatus.Red;
}

static LaunchStatus GetNavigationLaunchStatus()
{
Console.WriteLine("Getting navigation launch status...");
return LaunchStatus.Yellow;
}
}