C# Long

Value Long

Long type

In computers, numbers are stored as patterns of bits. Small numbers can be stored in fewer bits than larger numbers. The long type in the C# programming language contains 64 bits, which is 8 bytes, and can represent very large integral numbers but not floating-point numbers.

Keywords
System.Int64 information

long.MinValue = -9223372036854775808
long.MaxValue =  9223372036854775807

Example

Note

Let's begin by running this simple program. It shows that long variables can be positive or negative. Next, it shows the minimum value that can be stored in long, and the maximum value as well. It reveals that the long type is represented in 8 bytes—twice as many as an int. As with all numeric types, the default value is 0. And finally long is aliased to the System.Int64 struct internally.

This C# example shows the long number type. Long occupies 64 bits.

Program that uses long type [C#]

using System;

class Program
{
    static void Main()
    {
	long a = 100;
	long b = -100;

	// Long can be positive or negative.
	Console.WriteLine(a);
	Console.WriteLine(b);

	// Long is very long.
	Console.WriteLine(long.MinValue);
	Console.WriteLine(long.MaxValue);

	// Long is 8 bytes.
	Console.WriteLine(sizeof(long));

	// Default value is 0.
	Console.WriteLine(default(long));

	// Long is System.Int64.
	Console.WriteLine(typeof(long));
    }
}

Output

100
-100
-9223372036854775808
9223372036854775807
8
0
System.Int64

long.Parse

As with other numeric types in the C# language, the long type provides Parse and TryParse methods. long.Parse will throw exceptions if the input string is invalid. If there is a chance you have an invalid format, please use the TryParse method instead.

TryParse Overview
Program that uses long.Parse [C#]

using System;

class Program
{
    static void Main()
    {
	string value = "9223372036854775807";
	long n = long.Parse(value);
	Console.WriteLine(n);
    }
}

Output

9223372036854775807

Ulong

Ulong type

If the long type does not provide enough digits for you in the positive range, you can try the ulong type. You cannot have negative values with the ulong type. The largest positive integer that can be stored in the ulong type is 20 digits long.

Ulong Type

Summary

The C# programming language

We discovered some important parts of the long type. This type is twice as large as an int in the C# programming language, and four times as large as a short. Long has a narrow range of utility, mainly in programs where floating-point numbers are not required and regular integers are too small. If you need more positive values and no negative values, please consider the ulong type.

Number Examples
.NET