Lesson 2.9

long data type C# - programming example


Objective

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

What is long Data Types in C#?

"Long is signed 64-bit integer type data type which is derived from System.Int64 Class. It stores value between –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is used for storing very large number."

TypeRangeSize.Net Framework type
long–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Signed 64-bit integerSystem.Int64

Declaration and Initialization

You can declare long data type as follow:

		  
  long num1=85644; //or
  ulong num1=85644L //Adding suffix L
		  

Note:if the value of long 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 long_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            long 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:   long, float, double or decimal
  
  Example:
  
  long a=5, b=6;
  long result=a+b;

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

Passing long value as parameter

When calling a function which requires long 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 long_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            long 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 long value into int type before passing.
 long 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 'long_data_type.Program.ShowNumber(int)' has some invalid arguments
 Error 2: cannot convert from 'long' to 'int'
  

Summary

long is signed 64-bit integer type data type which stores value between –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is mostly used for storing very large numeric value. In the next chapter we will discuss on ulong data type.

BackNext