What does | (pipe) mean in c#?

前端 未结 6 1055
鱼传尺愫
鱼传尺愫 2020-12-03 17:23

Just wondering what the pipe means in this? ive never seen it before:

FileSystemAccessRule fullPermissions = new FileSystemAccessRule(
             \"Network         


        
相关标签:
6条回答
  • 2020-12-03 17:51

    I'm assuming you mean this: FileSystemRights.FullControl | FileSystemRights.Modify

    This FileSystemRights, is an enum with FullControl and Modify having their own numeric values.

    So if FullControl = 1 and Modify = 2,

    FileSystemRights.FullControl | FileSystemRights.Modify = 3.  
    00000001 | 00000010 = 00000011.  
    

    Each bit is a "flag" for the method. The input checks to see which "flag" is set and what to do.

    So in this example, position 1 (the digit all the way on the right in this case) is FullControl, and position 2 is Modify. The method looks at each of the positions, and changes it behavior. Using flags is a way of passing in multiple parameters of behaviors without having to create a parameter for each possiblity (e.g. bool allowFullControl, bool allowModify) etc.

    Bitwise Operator

    0 讨论(0)
  • 2020-12-03 17:56

    For an enum marked with the [Flags] attribute the vertical bar means 'and', i.e. add the given values together.

    Edit: This is a bitwise 'or' (though semantically 'and'), e.g.:

    [Flags]
    public enum Days
    {
         Sunday    = 0x01,
         Monday    = 0x02,
         Tuesday   = 0x04,
         Wednesday = 0x08,
         Thursday  = 0x10,
         Friday    = 0x20,
         Saturday  =  0x40,
    }
    
    // equals = 2 + 4 + 8 + 16 + 32 = 62
    Days weekdays = Days.Monday | Days.Tuesday | Days.Wednesday | Days.Thursday | Days.Friday;
    

    It's a bitwise-OR but semantically you think of it as an AND!

    0 讨论(0)
  • 2020-12-03 17:57

    It's a bitwise OR of two values, presumably it creates a FileAccessRule with both FullAccess and Modify permissions set.

    0 讨论(0)
  • 2020-12-03 17:57

    It's a binary operator:

    Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

    0 讨论(0)
  • 2020-12-03 18:03

    It's a boolean or. FullControl and Modify represent bits in a mask. For example 0001 and 0101. If you would combine those via pipe, you would get 0101.

    0 讨论(0)
  • It is normally a bitwise or operator. In this context, it's used on an enum with the flags attribute set.

    0 讨论(0)
提交回复
热议问题