What does | (pipe) mean in c#?

前端 未结 6 1058
鱼传尺愫
鱼传尺愫 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

提交回复
热议问题