C#
C#

help

Root Question Message

Soinagie
Soinagie3/6/2023
✅ stateful versus stateless methods?

Could someone please give me a simple example of these 2 methods as I don't really get what they do
Relevant
Relevant3/6/2023
So if you think about a method that is dependent on some value of a property of a class. Let's take a car for an example. You may have a public void Start() method. But this method may be dependent on the property public EngineType EngineType { get; set; }. This would be stateful because you can't perform that action without some knowledge about the object.
Soinagie
Soinagie3/6/2023
sorry but I don't know what {get; set} do yet
Relevant
Relevant3/6/2023
But let's say you have a Math class, with a method public int Add(int a, int b). This method does not depend on any other properties of the object and can be performed without knowledge of anything outside of the method.
Relevant
Relevant3/6/2023
Just ignore that part, then, it's just a property
Soinagie
Soinagie3/6/2023
it needs a and b
Relevant
Relevant3/6/2023
But those are provided when the method is called. It doesn't depend on any current state of the Math object
Relevant
Relevant3/6/2023
Let's say your Math class has a property name public int MyFavoriteNumber. The value of that property does not matter. It is not needed for the Add method
Soinagie
Soinagie3/6/2023
how can something be an object and class anyway? class is a category of somethings, object is something
Soinagie
Soinagie3/6/2023
like it doesn't make sense
Relevant
Relevant3/6/2023
a class is sort of like a set of rules for a type of thing. The object is the thing itself. Like the definition of a class might include something like a Color, Make, Model, and Year. That would be the class.

The object would be the actual instance of Car. So like a Red 1999 Ford Escort
Soinagie
Soinagie3/6/2023
so it does not depend on other properties of that class
Soinagie
Soinagie3/6/2023
not object
Soinagie
Soinagie3/6/2023
or is every class an object since it's an instance of itself?
Relevant
Relevant3/6/2023
no, a class is not an object
Relevant
Relevant3/6/2023
Though when I said "properties of the object", I mean that the properties of an object can actually have values. Properties of a class are just definitions of properties, usually don't have values
Relevant
Relevant3/6/2023
But that's kind of veering off course
Soinagie
Soinagie3/6/2023
so stateful methods get other values out of their class or somewhere else, but not from themselfs
stateless methods are 'self sufficient'
Relevant
Relevant3/6/2023
Stateful methods are dependent on the state of the object. Like my 1999 Ford Escort example. The Start() method might perform differently for that instance of Car versus a 2022 Tesla Model 3
Soinagie
Soinagie3/6/2023
what objects?
Soinagie
Soinagie3/6/2023
Im talking about methods
Relevant
Relevant3/6/2023
You're talking about methods dependent on object states
Soinagie
Soinagie3/6/2023
I'm asking about stateless and stateful methods
Relevant
Relevant3/6/2023
Correct
Relevant
Relevant3/6/2023
in other words, you're asking about methods that don't depend on object state versus methods that do depend on object state
Soinagie
Soinagie3/6/2023
what objects?
Soinagie
Soinagie3/6/2023
inside or outside of methods?
Relevant
Relevant3/6/2023
objects of the class that the method is contained in
Pobiega
Pobiega3/6/2023
you can't randomly store state inside a method as such.
Pobiega
Pobiega3/6/2023
you'll need either an object or a static variable to store that data in
Soinagie
Soinagie3/6/2023
what state?
Pobiega
Pobiega3/6/2023
ANY state
Soinagie
Soinagie3/6/2023
methods are sets of actions, not states
Pobiega
Pobiega3/6/2023
exactly
Relevant
Relevant3/6/2023
By state, we mean the state of the object. Or to word it differently, all of the values of all of the fields and properties at a given moment. That is it's state
Pobiega
Pobiega3/6/2023
so you cant store state (aka data that lives longer than a single execution of a method) in them
Soinagie
Soinagie3/6/2023
so object contains all of the values of their fields?
Pobiega
Pobiega3/6/2023
we've been over this in your previous threads, yes
Relevant
Relevant3/6/2023
Properties more specifically, but yes
Soinagie
Soinagie3/6/2023
I thought propeties were values?
Relevant
Relevant3/6/2023
properties hold values
Soinagie
Soinagie3/6/2023
could you give me an example of stateless and stateful methods please?
Relevant
Relevant3/6/2023
properties and fields are both just variables, but named a little different to describe how/where they're used
Soinagie
Soinagie3/6/2023
I thought they were the same
Soinagie
Soinagie3/6/2023
both are stored in a class
Pobiega
Pobiega3/6/2023
Consider these two classes and methods
Pobiega
Pobiega3/6/2023
public static class StatelessAdder
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

public class StatefulAdder
{
    private int _value;

    public int Add(int a)
    {
        _value += a;
        return _value;
    }
}
Pobiega
Pobiega3/6/2023
the top one is entirely stateless. any two executions given the same values in will return the exact same value out
Soinagie
Soinagie3/6/2023
Consider what?
Pobiega
Pobiega3/6/2023
read the code?
Soinagie
Soinagie3/6/2023
they're both classes
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy