Hi,
I am not sure about videos or other resources on this topic, but a very general description of stateful and stateless that may help is:
I am presuming that you have used the
Console.WriteLine(<anything to output>);
method before.
Breaking it down. The Console is the class you are calling, and the WriteLine is the method of the Console class.
The Console class you are calling is not a variable of a Console class. By this I mean, you have not had to create a new Console in your code, for instance.
Console myConsole = new Console();
and then you are not calling
myConsole.WriteLine(<anything to output>);
Therefore, the Console is stateless, as you do not (or have not) created a variable of Console. By extension the method
WriteLine(<anthing to output>);
is stateless, and you are calling a stateless method.
To explain a bit better.
Let us assume that you create a String variable:
string myString = "Some kind of message or what not.";
Now myString is a variable, and it has a state, among other things of:
- The string itself (Some kind of message or what not.)
- A Length, being how many characters are in the string.
You can find out the Length of the myString by calling
int myLength = myString.Length;
Even though the Length is a property and not a method, you cannot call the following:
int myOtherLength = string.Length;
as the ‘string’ above does not have any length, and therefore, no state.
As an example of a method, on the string class, when calling
Console.WriteLine(myString);
You are actually calling it this way:
Console.WriteLine(myString.ToString())
The compiler usually does all of this conversion (calling the toString() method) behind the scenes, so you don’t have to.
Side note: The toString() method converts the myString to a string (for printing in the console.)
Since you can call
myString.ToString(),
you would not be able to call
string.ToString();
since the String class ‘string’ is stateless, and does not have any actual ‘string’ associated with it.
Hopefully that did not confuse you too much with all of the string references.
When you get up to creating Classes, etc. The first class you make is the Program class, that is in the Program.cs file.
The basic construct of this class is:
{
public class Program
{
public static void Main(string[] args)
{
// You put stuff in here.
}
}
When you dotnet run a program, dotnet looks for a class that has a
public static void Main(string[] args)
method, and calls it. This is the code entry for any application, etc.
The keyword ‘static’ means that an instance of the class does not have to be instantiated before it can be called. dotnet would look to call
Program.Main(<args>);
and not have to call:
Program dotnetsProgram = new Program();
dotnetsProgram.Main(<args>);
The ‘static’ keyword means that the particular method can be called in a stateless environment.
Without ‘static’, you have to have an instance of the Object before calling a method on the Object.
Please note that the above explanation was a simple explanation, attempting to use examples of things that you already know at this particular time.
In time, you may find out that Console is not exactly stateless, as you can change the size of the console, meaning it has a particular state.
Happy programming.