3 / 3

"Very good, Teo. That's right. Knowing that, you probably can guess whether you can use variables in a constant declaration?"
"I'd guess no," I said, "because variables don't exist when the compiler tries to calculate the constant value."
"Bravo!" Ritchie said, sounding uncharacteristically pleased with me. Maybe it was because he realized he had found his tenth candidate for Wonderland. To sum it up: you can use constants to declare constants and variables, but you can't use variables to declare constants. Here is a code example:

double addMe = 2.01;
const double pi = 3.14159265358979323846; // Ok
const double e = 2.71828182845904523536; // Ok
const double ePlusPi = e + pi; // Ok - constant declaration through constants
const double ePlusAddMe = e + addMe; // Error: declaration of constant with a
 // variable

"Ritchie," I asked, "what types can I use with const ?"
"You can use all the types you've learned up to now, but not arrays. You can declare a constant int , double , float , char , bool , and string . "
"Thank you Ritchie." I said. "I think I've got the hang of what a const is."

"Two things to remember before we're done here." Ritchie said. "A constant can never be static , and you can't use const with var ."

const double pi = 3.14159265358979323846; // Ok
const var e = 2.71828182845904523536; // Error: can't use const with var
static const string Name = "Bob"; // Error: can't use const with static

"That's it Teo. Let's have you practice what you just learned. Head up to our virus production factory on the third floor and lend a hand."

developers problems