long (C# Reference)

The long keyword denotes an integral type that stores values according to the size and range shown in the following table.

Type

Range

Size

.NET Framework type

long

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Signed 64-bit integer

System.Int64

Literals

You can declare and initialize a long variable like this example:

long long1 = 4294967296;

When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In the preceding example, it is of the type long because it exceeds the range of uint (see Integral Types Table (C# Reference) for the storage sizes of integral types).

You can also use the suffix L with the long type like this:

long long2 = 4294967296L;

When you use the suffix L, the type of the literal integer is determined to be either long or ulong according to its size. In the case it is long because it less than the range of ulong.

A common use of the suffix is with calling overloaded methods. Consider, for example, the following overloaded methods that use long and int parameters:

public static void SampleMethod(int i) {}
public static void SampleMethod(long l) {}

Using the suffix L guarantees that the correct type is called, for example:

SampleMethod(5);    // Calling the method with the int parameter
SampleMethod(5L);   // Calling the method with the long parameter

You can use the long type with other numeric integral types in the same expression, in which case the expression is evaluated as long (or bool in the case of relational or Boolean expressions). For example, the following expression evaluates as long:

898L + 88

Note

You can also use the lowercase letter "l" as a suffix. However, this generates a compiler warning because the letter "l" is easily confused with the digit "1." Use "L" for clarity.

For information on arithmetic expressions with mixed floating-point types and integral types, see float and double.

Conversions

There is a predefined implicit conversion from long to float, double, or decimal. Otherwise a cast must be used. For example, the following statement will produce a compilation error without an explicit cast:

int x = 8L;        // Error: no implicit conversion from long to int
int x = (int)8L;   // OK: explicit conversion to int

There is a predefined implicit conversion from sbyte, byte, short, ushort, int, uint, or char to long.

Notice also that there is no implicit conversion from floating-point types to long. For example, the following statement generates a compiler error unless an explicit cast is used:

long x = 3.0;         // Error: no implicit conversion from double
long y = (long)3.0;   // OK: explicit conversion

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Keywords

Integral Types Table (C# Reference)

Built-In Types Table (C# Reference)

Implicit Numeric Conversions Table (C# Reference)

Explicit Numeric Conversions Table (C# Reference)

Int64

Concepts

C# Programming Guide

Other Resources

C# Reference