TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Long

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

Long. The long type contains 64 bits, or 8 bytes. It can represent very large integral numbers but not floating-point numbers.

It has a sign bit, so it supports positive and negative numbers.

System.Int64 information

long.MinValue = -9223372036854775808
long.MaxValue =  9223372036854775807

Example. First, we show that long variables can be positive or negative. We see the minimum value that can be stored in long, and the maximum value as well. The program reveals that the long type is represented in 8 bytes—twice as many as an int.

Int

Note: As with all numeric types, the default value is 0. And finally long is aliased to the System.Int64 struct internally.

C# program that uses long type

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

Parse. As with other numeric types, the long type provides Parse and TryParse methods. 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.

C# program that uses long.Parse

using System;

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

Output

9223372036854775807

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

Ulong

Summary. We investigated the long type. This type is twice as large as an int. It has a narrow range of utility. It helps in programs where floating-point numbers are not required and regular integers are too small.

Note: If you need more positive values and no negative values, please consider the ulong type.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf