❔ initiating Constructor
Why cant I sometimes initiate data members in a constructor like this:
Public Car (int wheels, string color)
{
Wheels = wheels
Color = color
}
And only use
this.color = color
this.wheels = wheels
What is the difference? Why do we have use this at all?
16 Replies
if you have
color = color
then you would set the local color to that which is setting itself with itself because the local variable shadows the instance property
if you have this.color = color
you say take the local color and set it on the instance property as you explicitly write the location of it
how is C# supposed to differentiate between the local variable and the property when they have the same nameEdit: why cant we only use default constructor
if you dont have a local variable color and only the
this.color
then you dont have to write this. as C# wont find locally a color so it searches anyway in the instance of the object for color as its not shadowed
this has nothing to do with constructors 👀Ahh I see, I only see this in constructors. Thats why I thought it had something to do with constructors
will have 0 effect
in your car example
What do you mean locally?
Sorry, Im new and very confused
variables have scopes where they live
and a strict order how C# searches for variables
for example you have
Then x is a local variable, it only exists in the scope of the bob method
you cant touch it from outside of the method
if you have
then x is a instance property ( i think that naming is incorrect )
when we combine now mister bob
then what will it print?
C# ( and pretty much any other OOP language )
searches as locally as possible
which means first it searches "do i have a local variable somewhere .. oh yes i have.. it has value 10"
so this will print 10
now .. "do i have a local variable.. no i dont.. do i have a instance variable? ( as its the scope above ) oh yes i have it has value 11"
but if you explicitly tell C# WHERE to search then it will take it from there
"you want the isntance variable of x.. it has 11 in it.. got you homie"
the brackets pretty much define the scope of variables
not entirely but for that you can remember that
Ahhh so much easier now. Thank you
So the instance variable of x would be 11, since we explicitly write this.x?
if you have inheritance then you have also the "base" scope
so you tell C# "look in the class where im inheriting from and search there the variable"
yes, i had a typo
is the property you're affecting named exactly as the param?
casing and all
yes thats what is about
yes exactly
if you use capitalized properties you won't have this issue
how shadowing works
thats another thing...this became over the years "less" important.. or atleast the cases you needed it went down
as C# has fancy syntax features to avoid it as its confusing
yes I avoid using
this
where I canbut its good to know as you will meet other shadowing problems in future
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.