Advertisement
Caminhoneiro

Flags Enum Example

Jul 16th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. [TestClass]
  2.     public class FlagsEnumExample
  3.     {        
  4.         [Flags]
  5.         enum Alignments
  6.         {
  7.             None = 0,
  8.             Top = 1,
  9.             Right = 2,
  10.             Bottom = 4,
  11.             Left = 8
  12.         }
  13.  
  14.  
  15.  
  16.  
  17.         [TestMethod]
  18.         public void Example()
  19.         {
  20.             // setting and evaluating
  21.             var topRightCombination = Alignments.Top | Alignments.Right;
  22.             var bottomLeftCombination = Alignments.Bottom | Alignments.Left;
  23.  
  24.  
  25.             var isTopIncluded = (topRightCombination & Alignments.Top) != 0;            
  26.             isTopIncluded = topRightCombination.HasFlag(Alignments.Top);
  27.  
  28.            
  29.  
  30.            
  31.             // combining combinations          
  32.             var all = topRightCombination | bottomLeftCombination;
  33.  
  34.             // Toggling values
  35.             all ^= Alignments.Top;
  36.            
  37.             all ^= Alignments.Top;
  38.            
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement