Tilde (~) Operator in the Enum definition in C#

Want to know what is the use of Tilde (~) symbol in C# especially when using enum definition?. Let’s have a look at the use of the Tilde (~) operator.

During a casual discussion with one of my friend , I came across a question on whether the tilde(~) symbol can be used in the enum definition as shown below.

Tilde (~) Symbol in the Enum definition in C#

public enum TypeData
{
    All = ~0,
    None = 0
}

Can tilde symbol be used as the enum values ? . Simple answer is Yes.

~0 results in the value -1

    1 Comment

  1. February 11, 2016
    Reply

    That’s because it’s the bitwise complement operator.
    https://msdn.microsoft.com/en-us/library/d2bd4x66.aspx

    Cheers,
    Daniel

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...