What is the tilde (~) in the enum definition?

2019-01-03 12:10发布

I'm always surprised that even after using C# for all this time now, I still manage to find things I didn't know about...

I've tried searching the internet for this, but using the "~" in a search isn't working for me so well and I didn't find anything on MSDN either (not to say it isn't there)

I saw this snippet of code recently, what does the tilde(~) mean?

/// <summary>
/// Enumerates the ways a customer may purchase goods.
/// </summary>
[Flags]
public enum PurchaseMethod
{   
    All = ~0,
    None =  0,
    Cash =  1,
    Check =  2,
    CreditCard =  4
}

I was a little surprised to see it so I tried to compile it, and it worked... but I still don't know what it means/does. Any help??

10条回答
萌系小妹纸
2楼-- · 2019-01-03 12:49

Just a side note, when you use

All = Cash | Check | CreditCard

you have the added benefit that Cash | Check | CreditCard would evaluate to All and not to another value (-1) that is not equal to all while containing all values. For example, if you use three check boxes in the UI

[] Cash
[] Check
[] CreditCard

and sum their values, and the user selects them all, you would see All in the resulting enum.

查看更多
做个烂人
3楼-- · 2019-01-03 12:57
public enum PurchaseMethod
{   
    All = ~0, // all bits of All are 1. the ~ operator just inverts bits
    None =  0,
    Cash =  1,
    Check =  2,
    CreditCard =  4
}

Because of two complement in C#, ~0 == -1, the number where all bits are 1 in the binary representation.

查看更多
做自己的国王
4楼-- · 2019-01-03 12:57

For others who found this question illuminating, I have a quick ~ example to share. The following snippet from the implementation of a paint method, as detailed in this Mono documentation, uses ~ to great effect:

PaintCells (clipBounds, 
    DataGridViewPaintParts.All & ~DataGridViewPaintParts.SelectionBackground);

Without the ~ operator, the code would probably look something like this:

PaintCells (clipBounds, DataGridViewPaintParts.Background 
    | DataGridViewPaintParts.Border
    | DataGridViewPaintParts.ContentBackground
    | DataGridViewPaintParts.ContentForeground
    | DataGridViewPaintParts.ErrorIcon
    | DataGridViewPaintParts.Focus);

... because the enumeration looks like this:

public enum DataGridViewPaintParts
{
    None = 0,
    Background = 1,
    Border = 2,
    ContentBackground = 4,
    ContentForeground = 8,
    ErrorIcon = 16,
    Focus = 32,
    SelectionBackground = 64,
    All = 127 // which is equal to Background | Border | ... | Focus
}

Notice this enum's similarity to Sean Bright's answer?

I think the most important take away for me is that ~ is the same operator in an enum as it is in a normal line of code.

查看更多
Rolldiameter
5楼-- · 2019-01-03 12:59

I'd think that:

[Flags]
public enum PurchaseMethod
{
    None = 0,
    Cash = 1,
    Check = 2,
    CreditCard = 4,
    All = Cash | Check | CreditCard
 }

Would be a bit more clear.

查看更多
该账号已被封号
6楼-- · 2019-01-03 13:00

Its better than the

All = Cash | Check | CreditCard

solution, because if you add another method later, say:

PayPal = 8 ,

you will be already done with the tilde-All, but have to change the all-line with the other. So its less error-prone later.

regards

查看更多
我只想做你的唯一
7楼-- · 2019-01-03 13:00

It's a complement operator, Here is an article i often refer to for bitwise operators

http://www.blackwasp.co.uk/CSharpLogicalBitwiseOps.aspx

Also msdn uses it in their enums article which demonstrates it use better

http://msdn.microsoft.com/en-us/library/cc138362.aspx

查看更多
登录 后发表回答