TheDeveloperBlog.com

Home | Contact Us

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

C# Ulong Type

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

Ulong. A ulong is an unsigned long type.

It is part of the System namespace. The ulong type offers an extremely large maximum value. It contains a full 64 bits of memory for storage. Despite its benefits, it is less common in practice.

Long

Example. The ulong keyword in the C# language is actually an alias to the System.UInt64 type in the base class library. Sometimes it is better to specify the UInt64 type instead as a reminder of the bit size.

Next: The program below shows how to declare a ulong and then access some of its static properties.

C# program that uses ulong

using System;

class Program
{
    static void Main()
    {
	//
	// Declare ulong variable.
	//
	ulong value1 = 100;
	Console.WriteLine(value1);

	// This won't compile:
	// value1 = -1;

	//
	// Look at MaxValue and MinValue.
	//
	Console.WriteLine(ulong.MaxValue);
	Console.WriteLine(ulong.MinValue);

	//
	// Parse string into ulong type.
	//
	ulong value2 = ulong.Parse("100");
	Console.WriteLine(value2);

	//
	// Compare two ulong values.
	//
	Console.WriteLine(value1 == value2);

	//
	// Write the typeof operator result.
	//
	Console.WriteLine(typeof(ulong));
    }
}

Output

100
18446744073709551615 <-- Max
0                    <-- Min
100
True
System.UInt64

You can declare a ulong in the same way as you can declare a regular integer. Usually, you will want much larger values than 100, which is shown in the program above. The ulong type is allocated on the stack when used as a local variable.

Tip: Sometimes you should specify the UL suffix on ulong constants. This eliminates confusion for the compiler.

Suffix

MaxValue property. This number is 20 digits long, which is adequate for many purposes even in the hard sciences. The ulong type uses all of its bits for storing a positive value. It has no sign bit reserved like the long type.

The minimum value of the ulong type is zero. This is because it has no sign bit, which is why it is called an unsigned value. In computer science, all numbers that are called unsigned can never represent negative numbers.

Parsing: You can use the static Parse and TryParse methods on the ulong type. Prefer TryParse if invalid data may be encountered.

Caution: The Parse method will throw System.FormatException when it is passed an invalid parameter.

FormatException

The type of the ulong reserved word is specified to be a System.UInt64 value type. These numeric types in the base class library are structs—they inherit from the System.ValueType struct first, and then from the object type.

Discussion. You can encounter this error when using ulong. When you compile programs, the default context is "checked", which means the compiler will verify the constant assignments and operators and issues an error if you have invalid code.

Error:

Constant value -1 cannot be converted to a ulong.

Performance—using a ulong type when one is not necessary will degrade performance. This is because the ulong will require twice as much memory as a System.Int32 type. This cost is incurred in method calls and field usages.

Summary. Ulong aliases the System.UInt64 type in the base class library. We saw the maximum and minimum values for this type. Ulong is a value type allocated on the stack—this influences program performance.


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