1. Declaring Variables
Let's quickly recap how to create variables. To create a variable, you need to write a command like this:
type name;
Examples:
| Command | Explanation |
|---|---|
|
Creates a variable s of type string. This variable can store text. |
|
Creates a variable x of type int. This variable can store whole numbers. |
|
Creates variables a, b, c, d of type int. These variables can store whole numbers. |
You can't create two variables with the same name in one code block. But in different blocks — you can. It's like boxes in different houses. More about code blocks — in the next lectures.
There are also some restrictions on the variable name. On one hand, it can be anything, but on the other — it can't have spaces, +, - and so on. It's best to use only Latin letters and numbers in variable names.
And again, remember that in C# it matters if you use uppercase or lowercase letters. int a is not the same as Int a.
By the way, in C# you can create a variable and assign it a value at the same time. This saves time and space:
| Compact code | Long code, equivalent to the left |
|---|---|
|
|
|
|
|
|
|
|
|
|
Way more compact and clear.
Now that we've figured out how to create variables, let's meet the two most used types in C#: int (whole numbers) and string (text/strings).
2. Type int
You can store whole numbers in a variable of type int: 5, -10, 100500. You can also do all sorts of math with int: add, subtract, multiply, divide, and more. Examples:
int x = 1; // x will be 1
int y = x*2; // y will be 2
int z = 5*y*y + 2*y + 3; // z will be 20+4+3, will be 27
int a = 5; // a will be 5
int b = 1; // b will be 1
int c = (a-b) * (a+b); // c will be 4*6, will be 24
int a = 64; // a will be 64
int b = a/8; // b will be 8
int c = b/4; // c will be 2
int d = c*3; // d will be 6
If you didn't skip math in school, this should all make sense 🧐
3. Type string
The string type lets you store text strings. To set a text string in C#, you write the text and put double quotes around it. Example:
| Code | Explanation |
|---|---|
|
s will contain the text Amigo |
|
s will contain the text 123 |
|
s will contain the text Bond 007 |
Looks easy, right? If so, here's another cool fact.
You can glue strings together in C# using the plus sign — +. Example:
string s1 = "Amigo" + " the best"; // s1 will contain Amigo the best
string s2 = ""; // s2 will contain an empty string — no characters at all
int x = 333;
string s3 = "Amigo" + x; // s3 will contain Amigo333
Check out the last example: we added a string and a number. It's simple: the number gets turned into a string, then the two strings are glued together. When you add strings and numbers, you always get a string.
4. Printing a Variable to the Screen
Seems pretty obvious and basic. So maybe you can already guess what command prints a variable to the screen?
It's actually super simple. To print something to the screen, we use the Console.WriteLine() command, passing in what we want to print as a parameter.
Console.WriteLine("Amigo"); // Amigo
Console.WriteLine("Ami" + "go"); // Amigo
string s1 = "Amigo";
Console.WriteLine(s1); // Amigo
string s2 = "Am";
Console.WriteLine(s2 + "igo"); // Amigo
Hope that makes things a bit clearer. And whether you really got it or not, let's check right now. Practice is the real test: only by practicing can you see if you really get it.
5. The Coolest Way to Print a Variable
Recently, C# got a new, awesome way to print variables to the screen. You can put variable names right in the text, and C# will stick their values right in the middle.
To do this, just put a $ sign before the first double quote and that's it. Example:
string name = "Alex";
int age = 25;
Console.WriteLine($"My name is {name}. I'm {age} years old."); // My name is Alex. I'm 25 years old.
We put a dollar sign before the string (before the quotes) and write variable names inside curly braces in the string. That's it. It works like magic. Compare:
string name = "Alex";
int age = 25;
//classic way
Console.WriteLine("My name is " + name + ". I'm " + age + " years old.");
//cool way
Console.WriteLine($"My name is {name}. I'm {age} years old.");
On CodeGym you're learning the newest version of C#, so you get all the cool stuff right away. Enjoy! 😎
GO TO FULL VERSION