TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
~ symbol in C# serves two distinct syntactical purposes depending on its context: as the unary bitwise complement operator and as the finalizer declaration syntax.
1. Bitwise Complement Operator
As a unary operator, ~ performs a bitwise logical negation on integral numeric types and enumeration (enum) types. It evaluates the binary representation of the operand and inverts each bit, converting 0 to 1 and 1 to 0.
Technical Mechanics:
- The operator is natively defined for
int,uint,long,ulong,nint,nuint, and allenumtypes. - When applied to smaller integral types (
byte,sbyte,short,ushort), the operand undergoes implicit numeric promotion tointbefore the bitwise inversion occurs. The result evaluates to anint. - When applied to an
enumtype, the operation evaluates to theenumtype itself. Unlike smaller integral types, applying~to anenumwith a smaller underlying type (e.g., anenumbacked by abyteorshort) returns theenumtype, not anint. - Because C# utilizes two’s complement representation for signed integers, applying
~to a signed integerxmathematically yields-x - 1.
~ declares a finalizer (historically referred to as a destructor). A finalizer is a specialized method invoked non-deterministically by the .NET Garbage Collector (GC) when processing the finalization queue before reclaiming the object’s memory.
Technical Mechanics:
- Finalizers can only be defined within
classorrecord classtypes. They are invalid instructorrecord structtypes. - The declaration cannot include access modifiers (e.g.,
public,private). - The declaration cannot accept parameters.
- The finalizer cannot be invoked explicitly by application code.
- During compilation, the C# compiler translates the
~ClassName()syntax into an override of theSystem.Object.Finalize()method, automatically wrapping the body in atry/finallyblock that ensuresbase.Finalize()is called.
Master C# with Deep Grasping Methodology!Learn More





