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 Systemusing System;// create classclass DecimalMaxValue{// main methodstatic void Main(){// print the field valueConsole.WriteLine(Decimal.MaxValue);}}
Code explanation
- Line 11: We print the value of the
Decimal.MaxValuefield to the console.
Any decimal value that tries to exceed the Decimal.MaxValue field will throw an error.
Code example
// use Systemusing System;// create classclass DecimalMaxValue{static void Main(){// add to another// decimal valuedecimal d1 = Decimal.MaxValue + 45.8695m;// print resultConsole.WriteLine(d1);}}
Code explanation
-
Line 10: We create a decimal variable and initialize it with the sum of
45.8695mandDecimal.MaxValue. This throws an error, because no decimal value is supposed to exceed theDecimal.MaxValuefield. -
Line 12: We print out the results to the console. But this throws an error, due to the reasons stated above.