Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does | (pipe) mean in c#?

Tags:

c#

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

FileSystemAccessRule fullPermissions = new FileSystemAccessRule(
             "Network Service",
             FileSystemRights.FullControl | FileSystemRights.Modify,
             AccessControlType.Allow);

Cheers

like image 551
Exitos Avatar asked Apr 18 '11 15:04

Exitos


3 Answers

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!

like image 132
Jackson Pope Avatar answered Oct 02 '22 13:10

Jackson Pope


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

like image 30
recursive Avatar answered Oct 02 '22 12:10

recursive


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

like image 45
cbz Avatar answered Oct 02 '22 13:10

cbz