What is Decimal.MaxValue in C#?

Overview

In C#, Decimal.MaxValue is a decimal field that represents the largest possible value of a decimal. It is constant and read-only, meaning that it cannot be changed.

Syntax

public static readonly decimal MaxValue;
Syntax to get the highest value of a decimal

Parameters

This field does not take any parameters. It is only invoked on the decimal object.

Return value

This field returns the positive value 79228162514264337593543950335.

Code example

// use System
using System;
// create class
class DecimalMaxValue
{
// main method
static void Main()
{
// print the field value
Console.WriteLine(Decimal.MaxValue);
}
}

Code explanation

  • Line 11: We print the value of the Decimal.MaxValue field to the console.

Any decimal value that tries to exceed the Decimal.MaxValue field will throw an error.

Code example

// use System
using System;
// create class
class DecimalMaxValue
{
static void Main()
{
// add to another
// decimal value
decimal d1 = Decimal.MaxValue + 45.8695m;
// print result
Console.WriteLine(d1);
}
}

Code explanation

  • Line 10: We create a decimal variable and initialize it with the sum of 45.8695m and Decimal.MaxValue. This throws an error, because no decimal value is supposed to exceed the Decimal.MaxValue field.

  • Line 12: We print out the results to the console. But this throws an error, due to the reasons stated above.

Free Resources