Read the syntax
Use arbitrary-size integer arithmetic when values exceed fixed-width integer types.
The result is the exact integer 2 to the power of 100, which exceeds long.MaxValue.
What it is and why to use it
Some exact integer calculations, such as counting combinations, quickly exceed long. System.Numerics.BigInteger grows its representation as needed, within available memory. It avoids fixed-width overflow for those integer operations, but it is not a floating-point or decimal-fraction type and is not an unlimited-resource calculation engine.
How it works
Construct a BigInteger from an integer, parse decimal text with a chosen culture, or use operations such as Pow. Ordinary arithmetic operators create results with the necessary integer representation. Division remains integer division. Avoid first computing an overflowing int expression and only then assigning it to BigInteger; make an operand a BigInteger before the calculation.
Common mistakes and limitations
Do not create huge values from unbounded user input without resource limits. Do not assume BigInteger solves fractional precision or cryptographic requirements. Converting a result back to a fixed-width integer can overflow, so check the range first.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
BigInteger value = BigInteger.Pow(new BigInteger(2), 100);Examples
Compute an exact power beyond long
Use BigInteger before exponentiation rather than overflowing a primitive.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Numerics;
using System.Globalization;
BigInteger value = BigInteger.Pow(new BigInteger(2), 100);
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
- The base is a BigInteger, so Pow performs arbitrary-size integer arithmetic.
- The result is the exact integer 2 to the power of 100, which exceeds long.MaxValue.
- An invariant format keeps the machine-readable decimal representation predictable.
- Expected output: 1267650600228229401496703205376
- Promote operands before a calculation that might overflow.
Check before converting to a smaller type
Treat a large parsed identifier as an exact number while protecting a narrowing conversion.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Numerics;
using System.Globalization;
BigInteger value = BigInteger.Parse("9223372036854775808", CultureInfo.InvariantCulture);
if (value >= long.MinValue && value <= long.MaxValue) {
Console.WriteLine((long)value);
} else {
Console.WriteLine("Outside Int64 range");
}
- The decimal string is one greater than long.MaxValue.
- BigInteger can represent it, but a conversion to long would overflow.
- The explicit bounds check selects the safe failure branch. If a value is an identifier rather than a quantity, keeping it as text may be more appropriate.
- Expected output: Outside Int64 range
- Promote operands before a calculation that might overflow.
Best practices
- Promote operands before a calculation that might overflow.
- Bound input sizes and check ranges before narrowing conversions.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference