This Was Not a Secret

Sometimes I think that Ritchie is an evil God. He knows everything that is going on in his base at any given time, how to program and control machines, and how to get things done in a post-apocalyptic world. What else could you need? That's why, one day when he surprised me with a visit to my room, my first thought was that he knew all about my secret business with Noname.

"Hi, Teo. How's it going?" Ritchie asked.
Quite well in fact; I've learned forbidden knowledge in the basement, made a deal with a machine controlled by you, and am desperately trying to leave this madhouse, I thought.

"Hi, Ritchie! I'm fine, how are you?" I said.
"Fine, thanks." he replied. "You have an opportunity to get a huge raise. You have a chance to move up to Wonderland."
"Wonderland?" I asked, hoping it sounded like I had never heard the term.

"It's the biggest resistance base on Earth. The best human programmers are there."
"Okay... but why me, Ritchie? You never seemed to like me much."
"And I still don't. But current circumstances require one more person. To put you in that slot, I need to train you more."

Programmers life

Some Things Never Change

"First, we need to go over constants in C#." Ritchie began.
In some tasks, you'll use values that never change. For example, the math constant Pi. Do you know the value?"
"Sure, 3.14..." I answered, hoping that it sounded like I was trailing off to save time rather than because I didn't know the rest of the digits.

"3.14159265... and so on and so forth. The main thing to know for this lesson is that it never changes. A hundred years ago, and a hundred years in the future, it still will be the same number."
"Got it." I said.
"C# has a special keyword that lets us work with values like this: const . You use const with the declaration of a new value to indicate that the value never changes, like this: "

const double pi = 3.14159265358979323846;

"It looks just like a variable declaration." I said.
"Yes," Ritchie replied, "it's almost the same thing. You just need to append the keyword, const , before the declaration. The difference is that you can never change the value that you declare with const . It is no longer a variable , but rather a constant ."

const double pi = 3.14159265358979323846;
var a = pi * 2; // Ok, we are using constant in an expression
pi = 3.14; // Compilation error! Can't change constant

"Constants are compile-time entities ." he continued. "When the compiler sees a constant in your code, it replaces all usages of that constant with the actual value of the constant. Look here:"

How you write code How compiler changes it
const int facesCount = 6;
int side = 4;
var square = facesCount * side * side;
int side = 4;
var square = 6 * side * side;
const string name = "John";
Console.WriteLine(name);
Console.WriteLine("John");

"Is this clear, Teo?" Ritchie asked.
"Yes, I answered. " The compiler takes the value of the constant and writes it directly in every place where the constant is used before the program starts to execute. "
"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."

c sharp developer creates Problems

At Codeasy we believe that learning should be fun and engaging. Please reach out to us to share your feedback and collaboration ideas.