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.
public static readonly decimal MaxValue;
This field does not take any parameters. It is only invoked on the decimal object.
This field returns the positive value 79228162514264337593543950335
.
// use Systemusing System;// create classclass DecimalMaxValue{// main methodstatic void Main(){// print the field valueConsole.WriteLine(Decimal.MaxValue);}}
Decimal.MaxValue
field to the console.Any decimal value that tries to exceed the Decimal.MaxValue
field will throw an error.
// use Systemusing System;// create classclass DecimalMaxValue{static void Main(){// add to another// decimal valuedecimal d1 = Decimal.MaxValue + 45.8695m;// print resultConsole.WriteLine(d1);}}
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.