diff --git a/snippets/csharp/keywords/Program.cs b/snippets/csharp/keywords/Program.cs index e58551c2faa..c4daa3bc3e6 100644 --- a/snippets/csharp/keywords/Program.cs +++ b/snippets/csharp/keywords/Program.cs @@ -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(); } } } diff --git a/snippets/csharp/keywords/TrueFalseLiteralsExamples.cs b/snippets/csharp/keywords/TrueFalseLiteralsExamples.cs new file mode 100644 index 00000000000..0820136b8a9 --- /dev/null +++ b/snippets/csharp/keywords/TrueFalseLiteralsExamples.cs @@ -0,0 +1,33 @@ +using System; + +namespace keywords +{ + public static class TrueFalseLiteralsExamples + { + public static void Examples() + { + TrueExample(); + FalseExample(); + } + + private static void TrueExample() + { + // + bool check = true; + Console.WriteLine(check ? "Passed" : "Not passed"); + // Output: + // Passed + // + } + + private static void FalseExample() + { + // + bool check = false; + Console.WriteLine(check ? "Passed" : "Not passed"); + // Output: + // Not passed + // + } + } +} \ No newline at end of file diff --git a/snippets/csharp/keywords/TrueFalseOperatorsExample.cs b/snippets/csharp/keywords/TrueFalseOperatorsExample.cs new file mode 100644 index 00000000000..17a771ccd98 --- /dev/null +++ b/snippets/csharp/keywords/TrueFalseOperatorsExample.cs @@ -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; + } + + 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; + } +}