Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "~" mean before enums

Tags:

syntax

c#

enums

Today I see this code:

ViewBag.country = from p in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures)
                              select new SelectListItem
                              {
                                  Text = p.EnglishName,
                                  Value = p.DisplayName
                              };

And I can not understand. "~" - This is a mistake? As far as I remember, "~" is placed before the destructors. But this is enum. And this code compiled!

like image 396
CMaker Avatar asked Apr 12 '13 09:04

CMaker


People also ask

What do you mean by enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

What is meant by enum value?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.

Why do we use enum?

Enums are used when we know all possible values at compile time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time. A Java enumeration is a class type.

Do enum values start at 0 or 1?

The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.


1 Answers

It's bitwise negation operator.

~ Operator (C# Reference)

The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are predefined for int, uint, long, and ulong.

And because operations on integral types are generally allowed on enumeration you can use ~ with enums backed with types listed above.

like image 164
MarcinJuraszek Avatar answered Sep 19 '22 11:09

MarcinJuraszek