Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
BigInteger Class in C#
The BigInteger class in C# is designed to handle arbitrarily large integers that exceed the limits of standard integer types. It is part of the System.Numerics namespace and provides support for mathematical operations on very large numbers without overflow.
Unlike built-in integer types like int or long, BigInteger can represent integers of any size, limited only by available memory. This makes it ideal for cryptographic calculations, mathematical computations, and scenarios requiring precise arithmetic with large numbers.
Syntax
The BigInteger structure declaration −
[SerializableAttribute] public struct BigInteger : IFormattable, IComparable, IComparable<BigInteger>, IEquatable<BigInteger>
Creating a BigInteger instance −
BigInteger num = new BigInteger(value);
BigInteger num = BigInteger.Parse("12345678901234567890");
BigInteger Constructors
| Constructor | Description |
|---|---|
| BigInteger(Byte[]) | Creates a new BigInteger using values from a byte array |
| BigInteger(Decimal) | Creates a new BigInteger using a Decimal value |
| BigInteger(Double) | Creates a new BigInteger using a double-precision floating-point value |
| BigInteger(Int32) | Creates a new BigInteger using a 32-bit signed integer value |
| BigInteger(Int64) | Creates a new BigInteger using a 64-bit signed integer value |
Creating BigInteger Instances
Example
using System;
using System.Numerics;
class Program {
public static void Main() {
// Creating BigInteger from different data types
BigInteger num1 = new BigInteger(123456789);
BigInteger num2 = new BigInteger(double.MaxValue);
BigInteger num3 = BigInteger.Parse("98765432109876543210");
Console.WriteLine("From int: " + num1);
Console.WriteLine("From double.MaxValue: " + num2);
Console.WriteLine("From string: " + num3);
// Creating very large number using multiplication
BigInteger largeNum = BigInteger.Multiply(Int64.MaxValue, Int64.MaxValue);
Console.WriteLine("Large number: " + largeNum);
}
}
The output of the above code is −
From int: 123456789 From double.MaxValue: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368 From string: 98765432109876543210 Large number: 85070591730234615847396907784232501249
BigInteger Operations
Example
using System;
using System.Numerics;
class Program {
public static void Main() {
BigInteger a = BigInteger.Parse("123456789012345678901234567890");
BigInteger b = BigInteger.Parse("987654321098765432109876543210");
// Arithmetic operations
Console.WriteLine("Addition: " + BigInteger.Add(a, b));
Console.WriteLine("Subtraction: " + BigInteger.Subtract(b, a));
Console.WriteLine("Multiplication: " + BigInteger.Multiply(a, 2));
Console.WriteLine("Division: " + BigInteger.Divide(b, 10));
// Power operation
BigInteger power = BigInteger.Pow(2, 100);
Console.WriteLine("2^100: " + power);
}
}
The output of the above code is −
Addition: 1111111110111111111011111111100 Subtraction: 864197532086419753208641975320 Multiplication: 246913578024691357802469135780 Division: 98765432109876543210987654321 2^100: 1267650600228229401496703205376
BigInteger vs Standard Integer Types
| Aspect | Standard Types (int, long) | BigInteger |
|---|---|---|
| Size Limit | Fixed (32-bit, 64-bit) | Unlimited (memory-dependent) |
| Performance | Fast (hardware supported) | Slower (software implementation) |
| Memory Usage | Fixed and minimal | Variable, depends on number size |
| Overflow | Can overflow silently | Never overflows |
Conclusion
The BigInteger class in C# provides unlimited precision integer arithmetic, making it essential for applications requiring calculations with very large numbers. While it offers flexibility and prevents overflow, it comes with performance trade-offs compared to standard integer types, so use it when precision with large numbers is more important than speed.
