Welcome back.
In this video we're going to look at fields okay.
So fields are variables that are created outside of methods inside of a class.
So you can create variables at all kinds of areas.
Right.
You can create them inside of methods.
You can create them outside of classes even even though that's a little more tricky.
And you can create them inside of classes, but outside of methods.
Let me show you what I mean with this.
So for example we have this variable Myresult right?
And we're using the result every single time.
So what we're doing is we're creating a new variable in each of those methods.
So what we could do instead is we could create one field.
So one variable that is also called field.
For example my result.
And we don't need to assign a value to it.
We can assign a value later on.
So for example in this case I don't need my result from here anymore because this my result will be
available inside of the main method.
So now this my result here is the same as this up here.
And we assign a value to it.
And then we can use it throughout the entire application.
So that's the thing with this variable it can be used in all other methods.
Now it can be used inside of the main method.
It can be used inside of the add two values.
So instead of creating a new result variable I can just use the my result variable and I can just assign
the value there.
The same goes obviously for the return I need to return my result.
Then instead of result because result doesn't exist anymore, but I still need to return a int here
and the same goes down here.
So I can just say overwrite my result instead of creating a new variable.
And I can obviously return my result here as well.
So my result now is a field.
Now you can call it a global variable, even though it is not super specific or not very precise.
Global variable is two degree still correct.
And you can use that term because it is a variable that is globally available, at least inside of our
class.
But the more specific term is field or instance variable.
Okay.
So this is a field or instance variable.
So both of those are fine term's uh sometimes even called global variables.
Even though as I said this is a little more of a wider term.
Global variables are usually variables that are usable throughout the entire application.
So in our case, as our application is only one class, we can say that this is a global variable.
But once we have multiple classes we will look at global variables with a different perspective again.
And then we can see how we can create actually global variables.
And now in this context I want to show you our old program.cs that we used before.
We used this very old style or the old style Program.cs file is that this int my result that I created
earlier.
It is now just for your context, we are at the old file, the one that we used before.
This is the file with the old style, and this is the file with the new style that we used before.
Okay, so when I created this variable into my result, it was the same as if I created a variable inside
of this main method.
Right.
Because everything that we did in here was basically inside of the main method.
Okay.
And now what we're doing is we are inside of a class where the main method is just the starting point,
the entry point of our program.
And now we're creating variables outside of the main method which then are called fields.
So inside of the new style so to speak, with having the top level statements, I cannot go ahead and
create a field because if you hover over my result you can see it says local variable int my result.
So this variable is only locally.
So inside of the main method so to speak available.
Now if we look at this one here it says field.
So in brackets it is a field now.
So this is a variable that is available inside of the entire program file.
So inside of this entire class called program.
And it can be used not only in the main method but it can also use in other methods.
Okay.
So just to clarify this once again.
So I would say we are really ready to go ahead and use the old style a lot more.
And this will be necessary for what's to come.
So once we are looking at classes and creating different objects, we need to fully understand how this
works and use it a lot so it will become a lot more.
Unnatural overtime, but let's just go from one step to the other.
So see you in the next video.