Lesson 2.10

ulong data type C# - programming example


Objective

What is ulong data type in c#?
How to declare and initialize
Programming examples
Conversion and Casting

What is ulong Data Types in C#?

"Ulong is unsigned 64-bit integer type data type which is derived from System.UInt64 Class. It stores value between 0 to 18,446,744,073,709,551,615. It is used for storing very large number."

TypeRangeSize.Net Framework type
ulong0 to 18,446,744,073,709,551,615Unsigned 64-bit integerSystem.UInt64

Declaration and Initialization

You can declare ulong data type as follow:

		  
  ulong num1=85644; //or
  ulong num1=85644U //Adding suffix U
		  

Note:if the value of ulong variable exceed from its range a compilation error occurs:

Integral constant is too large

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ulong_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong num1, num2, result;
            int result1;
            num1 = 20;
            num2 = 30;
            result = num1 * num2; //Implicit Conversion
            result1=(int)(num1 * num2); //Explicit Conversion
            Console.WriteLine(result);
            Console.WriteLine(result1);
            Console.ReadLine();
        }
    }
}

		  

Output

600
600
_

Conversion

1. Implicit Conversion - No need to convert when left hand side data type 
  is:   float, double or decimal
  
  Example:
  
  ulong a=5, b=6;
  ulong result=a+b;

2. Explicit Conversion - You need to convert when left hand side data type is: byte, sbyte, short, ushort, int, uint, long and char Example: ulong a=5, b=6; int result=(int)(a+b);

Passing ulong value as parameter

When calling a function which requires ulong value in parameter you must cast value into respective datatype to avoid compilation error..

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ulong_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong var1=342;
            ShowNumber((int)(var1)); // Correct(√)

            ShowNumber(var1); // Incorrect(×)
        }

        public static void ShowNumber(int num1)
        {
            Console.WriteLine(num1);
            Console.ReadLine();
        }
    }
}
		  

Output

342
_

It is best habbit to cast or convert data into respective datatype everywhere. It will save you from unnecessary runtime errors and makes your code more robust.

Explanation

In the above example we have created a function called ShowNumber(int num1) which requires an integer type value as parameter and then display the number.

We have called this function in our main class in which we casted ulong value into int type before passing.
 ulong var1=342;
 ShowNumber((int)(var1)); // Correct(√)

 If we don’t cast it will raise compilation error as follow :
 ShowNumber(var1); // Incorrect(×)

 Compilatation Error :Error 1: The best overloaded method match for 'ulong_data_type.Program.ShowNumber(int)' has some invalid arguments
 Error 2: cannot convert from 'ulong' to 'int'
  

Summary

ulong is unsigned 64-bit integer type data type which stores value between 0 to 18,446,744,073,709,551,615. It is mostly used for storing very large numeric value. In the next chapter we will discuss on float data type.

BackNext