Equivalent of SQL Bigint in C#

Syed Hassan Sabeeh Kazmi Jan 30, 2023 Jun 28, 2022
  1. Use the BigInteger Struct in C# as the Equivalent of SQL bigint
  2. Use long or int64 as the Equivalent of SQL bigint in C#
Equivalent of SQL Bigint in C#

The bigint data type in SQL is the 64-bit representation of an integer. It takes up 8 bytes of storage and can range from -2^63 (-9,223,372,036,854,775,808) to 2^63 (9,223,372,036,854,775,807).

It represents an extremely big number and storing these type of numbers require something similar in C#. In this tutorial, you will learn what data type to use as an equivalent of a bigint in C#.

In C#, all numeric data types store a limited range of values. Furthermore, to remove maximum and minimum number limitations, C# includes the BigInteger data type representing an arbitrarily large signed integer with no upper or lower limits.

Use the BigInteger Struct in C# as the Equivalent of SQL bigint

The BigInteger is an immutable struct type with no maximum or minimum value limitations. It is a part of the System.Numerics namespace and theoretically has no upper or lower bounds.

Its members or data is closely parallel to other integral types in C#.

It’s different from other integral types in the .NET framework because it has no MinValue and MaxValue properties. It enables you to perform primary mathematical operations by overloading the standard numeric operators.

using System;
using System.Numerics;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        // declaring a BigInteger
        // Use new keyword to instantiate BigInteger values

        // it can store a value from a double type
        BigInteger number1 = new BigInteger(209857.1946);
        Console.WriteLine(number1 + "");

        // it can store a value from an Int64 type
        BigInteger number2 = new BigInteger(947685917234);
        Console.WriteLine(number2);
    }
}

Output:

209857
947685917234

Use long or int64 as the Equivalent of SQL bigint in C#

The long data type in C# represents a 64-bit or 8 bytes integer and is similar to a bigint. It can represent extremely large positive and negative integral numbers.

It’s an immutable value type that represents signed integers with values that range from negative 9,223,372,036,854,775,808 (represented by the Int64.MinValue constant) through positive 9,223,372,036,854,775,807 (represented by the Int64.MaxValue constant).

using System;

public class dataTypeforBI
{
    public static void Main(string[] args)
    {
        long number1 = -64301728;
        Console.WriteLine (number1 + "");

        long number2 = 255486129307;
        Console.WriteLine (number2);
    }
}

Output:

-64301728
255486129307
Syed Hassan Sabeeh Kazmi avatar Syed Hassan Sabeeh Kazmi avatar

Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.

GitHub

Related Article - Csharp Data Type