Hi,
Admittedly, the code is a bit confusing but Vector2Int coordinates
and bool isWalkable
do not refer to the variables of the same name at the top of your class.
The reason is the so-called scope. The top-level is the instance. And the instance is behind the this
keyword. You can regard it as a variable referencing the current instance/object of the class.
The variables in the parentheses of a method are the so-called method parameters. They act as local variables. These variables exist only within the scope of the method code block. Other methods cannot access them.
this.coordinates = coordinates;
are two different variables. To be able to distinguish between them, we use the this
keyword. this.coordinates
refers to the variable at the top of your code while coordinates
(in the context of the method code block) refers to the parameter of the same name.
If you used, for example isPath
in the method code block, you do not need this
. this
would be implicite since there is no other variable within the current scope. You may add this
, though.
Regarding pure classes and constructors, what exactly would you like to know? The constructor just creates an object of a class. The class could be empty.
public class Test
{
// constructor
public Test()
{
}
}
// In another method somewhere else
Test test1 = new Test();
Test test2 = new Test();
That’s the simplest pure class. No inheritence (apart from the basic C# stuff like the Object class, which is always available), no dependencies on anything but the basic C# stuff which is always available.
And if you use C# built-in types (also called “primitive data types”) only, the class remains pure.
Is this what you wanted to know?
See also: