In this video, we're going to look at how we can declare a variable, how we can assign a value to
that variable, how we can overwrite the variable and access the variable.
So a bunch of words that I just used, let's look at it step by step.
So number one is going to be to declare a variable.
And you can see that I'm using the double slash here in order to create a comment.
So this is code that will not be run by the compiler.
It is just for us to see what's going on and to understand what a piece of code does.
So I'm saying that I want to create a variable that will be called my friend's name.
And here I'm declaring it.
So I'm declaring a and you could say string variable, which means a variable that holds the data type
string.
So it can only hold information that will be of the type string.
We will see what that means later on when we are using other data types and compare them to each other.
So then I can assign a value to my friend's name.
I can say something like Yannick for example.
So you see in a new line I used the same name as this variable's name.
Then I used the equal sign and assigned a string to it.
So this here is a string.
How do I know that it is a string?
That's because of those quotation marks.
So you start a string with quotations and you finish a string with quotations.
So you need to surround a string with quotations.
So your text, so to speak, that you want to store as text inside of a variable, which is just a space
inside of your memory, inside of your memory of the chip or the computer that you're using.
So now that is where I assigned a value to the my friend's name variable.
And you see, each time I end with a semicolon.
So when I declared a variable, I finished the statement with a semicolon.
When I assigned a value to that variable, I used a semicolon here.
And now I'm going to use that name.
So I'm going to say use the variable.
And I'm going to do that inside of a Console.WriteLine statement.
So in case you're wondering I'm using c w I'm pressing the tab key.
So this is a code snippet which is pretty cool because now we can just get this piece of code with just
two letters and a tab.
So now inside of the brackets here of this write line statements I can write the variable name.
So I can just say something like my friend's name.
And this is how I access the variable.
So use slash access the variable.
Which means that I'm just going to now have whatever is inside of the variable be represented in here.
So that just means that right line which is printing out text.
And guess what.
My friend's name, this variable is text.
So now it can handle that.
And it will display that text onto the console.
So you see it says hello world.
And then Yannick okay.
So we can get rid of this here and also of this line.
And let's run it again.
And it will just say Yannick.
So how do I now override this name.
So variables in C sharp default variables like the one that we have here can be overwritten okay.
So I can just say my friend's name is equal to.
And then let's say now Frank is my new friend okay I'm not friends with Yannick anymore.
Sorry Yannick.
So now I can use this same line that I used earlier and I can run it again.
And now it will say Frank.
So it says Yannick first.
Then I found the new friend and my new friend's name is Frank.
Okay, so here I am overriding the variable value.
Okay.
We're assigning a new value to that variable.
So we just see that this is just holding whatever we are assigning to it.
And whenever we're using this variable it's just giving us whatever is inside of it.
So here we could have just said Frank.
Right.
It would display the same thing.
But what if we don't assign the value of Frank manually inside of our code, but we get it from a database
or so.
That's where variables are becoming very useful.
So variables are really important.
They allow you to store data inside of your memory and reuse it in your code every single time that
you need it.
Just for your information, you could have also declared and assigned a value to your variable in the
same line.
So we didn't need to do this in two lines.
We could have done it in one line too.
So instead of saying string my friends name, semicolon my friends name.
Is equal to Yannick.
We could have said string.
My friends name two is equal to Frank straight away.
Like this for example.
So we could have just done that.
This is where we are declaring a variable and assigning a value to it.
So that's another alternative of doing it.
So you see that we use the naming convention where the variable name starts with the lower letter.
And each new word goes on with a capital letter.
Okay.
So that's it for this video.
See you in the next one.