Wonder_land

Wonderland

One day while walking past Ritchie's room I overheard an interesting conversation. There was a woman's voice, and oddly, it sounded like she was exerting authority over Ritchie. I had only ever known Ritchie as the leader of the resistance, and it was hard to imagine who he would let talk to him in such a tone.

"You need to send ten more people to Wonderland." she said.
"I can only spare nine. We can't keep sending you ten people every month. We don't have that many people!" Ritchie replied, almost begging.

"Ritchie, you know that everything we do is for the human race. We need people if we want Project Rust to succeed. This is our last hope to conquer the machines!"
"I know, I know." Ritchie replied. "But I just don't have a tenth person for you. We're already running on a skeleton crew as it is."

"What about the one you pulled forward through time; Teo was his name, I think?" she asked.
"He wouldn't do. He's learning very fast, but he's still a novice. If you ask him what something as simple as a var or const is he'll be in a state of complete mental stupor for an hour." Ritchie said. I know I'm not the best coder, but I personally thought his words were a bit harsh.

"Let me be the judge of that." the woman replied, "I'll evaluate him tomorrow."
Guessing that this was near the end of the conversation, I quickly hurried on my way. Receiving prior news of a surprise exam is never bad, but I was still confused as to who the woman was and what they were talking about. I'd have the answer to the former question within 24 hours, and it seemed like the answer to the latter, for better or worse, would depend on how I measured up to the woman's evaluation. 'Wonderland' and 'Project Rust' meant nothing to me, but I did want to prove to Ritchie that he was wrong about me. I'm smarter and had learned much more than just what he had taught me to this point. To prove it, I needed to learn those terms Ritchie had used: var and const . I decided to seek Tom out and see if he could give me a quick lesson.

Type Does Not Matter

Tom was happy to see me, and luckily, willing to teach me about var and const . I had been a little worried that these would be part of the codes that were reserved for the resistance group's leaders.

"As you know, Teo," Tom started, "in C#, every variable and function return value has a type."
"Yes," I said, "we need to specify a type such as int , string , double , or others."
"Perfect. I see that you've learned some things after I saw you last time! Sometimes the type can be quite long. For example, to create a double array, your code would look like this." Tom said, opening his laptop.

double[] someNumbers = new double[5];

And here's an example of an even longer array:

IDictionary<IEnumerable<string>, int> omg = new Dictionary<IEnumerable<string>, int>();

For simplicity and less typing in C#, there's a special type replacement token, var . Declaring a local variable using var is just a syntactical shortcut that forces the compiler to infer the specific type from your expression. The following code does the same as the first code shown above, but with much less repetition.

var someNumbers = new double[5];

When you assign new double[5] to a variable someNumbers , the compiler knows you're creating an array of doubles and automatically makes the type of the array itself match that of its values; in this case, double [] . It is very important to understand that using var does not change the meaning of the code at all, it just changes how the code looks. Here are a few pairs of code examples that are written differently, but do the same thing:

// These two lines of code are equivalent:
int[] someIntegers = new int[5];
var someIntegers = new int[5];

// And these as well:
string myString = "Ritchie has secrets from me";
var myString = "Ritchie has secrets from me";

// And these:
float radius = 15.789f;
var radius = 15.789f;

"Are you following me, Teo?" Tom asked.
"Yes," I answered, "but I have a question: can I replace any type with var ?"
"Yes, Teo. You can replace any type with var as long as the compiler can understand what type you mean. Here are some constraints for using var :"

  1. var can only be used for local variables inside methods. You can not use var for a method's return type or a method's parameter type.

    public static int Add(int a, int b) // This declaration is OK
    {...}
    
    public static var Add(int a, int b) // NOT OK: var as a return type
    {...}
    public static int Add(var a, var b) // NOT OK: var as a parameter's type
    {...}
    
  2. The variable must be initialized with a value if it is declared with var .

    var someIntegers = new int[5]; // OK
    var someIntegers; // NOT OK - no initial value
    var radius = 15.789; // OK
    var radius; // NOT OK - no initial value
    

"That just about covers var . Any more questions?" Tom asked.

"When should I use it?" I replied. " Is it considered good style to declare every local variable with var ? "
"That depends on your coding style. Some programmers prefer to use var for all possible local variable declarations. Others say that it should not be used for variables that hold a function return value. Finally, a small minority of programmers prefer to never use var at all. I would suggest using it wherever you can to start, and when you're comfortable with it, re-evaluate. "

"Thanks Tom," I said, "I'll use var where I can."
"Good. Here are some excersises to get you more familiar with var ."

Keep coding