- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Added snippets for true and false operators/literals #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            BillWagner
  merged 2 commits into
  dotnet:master
from
pkulikov:true-false-operators-snippet
  
      
      
   
  Dec 5, 2018 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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> | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
| } | ||
|  | ||
| 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; | ||
| } | ||
| } | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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 & zwork correctly whenzisYellow, this should returnYellowwhen one ofxoryisYellow.Do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner
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.
There was a problem hiding this comment.
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, andzvalues: now the&operator is commutative and associative. In particular, that means thatx1 & ... & xNisYellowiff there is only one operand which isYellowwith all others beingGreen. That's why I've updated thetruedefinition to returntruevalue forYellowoperand. The following requirement is met: