Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The long keyword in C# is a built-in value type that serves as an alias for the .NET System.Int64 structure. It represents a 64-bit (8-byte) signed integer, utilizing two’s complement representation to store numerical data.

Technical Specifications

  • Underlying Type: System.Int64
  • Memory Size: 64 bits (8 bytes)
  • Sign Bit: The most significant bit (MSB) denotes the sign (0 for positive, 1 for negative).
  • Minimum Value: -9,223,372,036,854,775,808 (long.MinValue)
  • Maximum Value: 9,223,372,036,854,775,807 (long.MaxValue)
  • Default Value: 0L

Syntax and Literals

By default, the C# compiler treats numeric literals as int if the value falls within the 32-bit signed integer range. To explicitly instruct the compiler to treat a literal as a long, append the L or l suffix. The uppercase L is standard convention to prevent visual confusion with the digit 1.
// Standard declaration and initialization
long a = 100000;

// Explicit literal typing using the 'L' suffix
long b = 9223372036854775807L;

// Hexadecimal literal
long c = 0x7FFFFFFFFFFFFFFFL;

// Binary literal (C# 7.0+) with digit separators
long binLiteral = 0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L;

Type Conversions

Because long is a 64-bit type, it participates in specific implicit and explicit conversion rules within the C# type system.

Implicit Conversions

C# allows implicit conversions to long from any integral type with a smaller bit-width or an unsigned 32-bit type. A long can also be implicitly converted to floating-point types (float, double) and the decimal type. Conversion to floating-point types may result in a loss of precision, whereas conversion to decimal retains full precision.
int intValue = 2147483647;
long implicitLong = intValue; // Implicit conversion from int to long

// Implicit conversion to floating-point types
float fValue = implicitLong;        // Compiles, but may lose precision
double doubleValue = implicitLong;  // Compiles, but may lose precision

// Implicit conversion to the decimal type
decimal decimalValue = implicitLong; // Compiles, no precision lost

Explicit Conversions (Casting)

Conversions from types that exceed the maximum value of long (like ulong), or from floating-point and decimal types (which truncate the fractional part), require an explicit cast.
ulong unsignedLong = 9223372036854775808ul;
long castedLong = (long)unsignedLong; // Explicit cast required

double fractionalDouble = 1234.56;
long truncatedLong = (long)fractionalDouble; // Evaluates to 1234

Memory Layout

In memory, a long is stored as a contiguous 64-bit block. The size of the type can be evaluated at compile-time using the sizeof operator, which yields a constant value and does not require an unsafe context for built-in types.
int size = sizeof(long); // Evaluates to 8
Master C# with Deep Grasping Methodology!Learn More