C
C#16mo ago
McMahone

❔ 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
Joreyk ( IXLLEGACYIXL )
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 name
McMahone
McMahoneOP16mo ago
Edit: why cant we only use default constructor
Joreyk ( IXLLEGACYIXL )
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 👀
McMahone
McMahoneOP16mo ago
Ahh I see, I only see this in constructors. Thats why I thought it had something to do with constructors
Joreyk ( IXLLEGACYIXL )
public void PaintCar(string color)
{
color = color;
}
public void PaintCar(string color)
{
color = color;
}
will have 0 effect in your car example
McMahone
McMahoneOP16mo ago
What do you mean locally? Sorry, Im new and very confused
Joreyk ( IXLLEGACYIXL )
variables have scopes where they live and a strict order how C# searches for variables for example you have
public void Bob()
{
int x = 10;
}
public void Bob()
{
int x = 10;
}
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
public class Mister
{
int x = 11;

}
public class Mister
{
int x = 11;

}
then x is a instance property ( i think that naming is incorrect ) when we combine now mister bob
public class Mister
{
int x = 11;
public void Bob()
{
int x = 10;
Console.WriteLine(x);
}
}
public class Mister
{
int x = 11;
public void Bob()
{
int x = 10;
Console.WriteLine(x);
}
}
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
public class Mister
{
int x = 11;
public void Bob()
{
Console.WriteLine(x);
}
}
public class Mister
{
int x = 11;
public void Bob()
{
Console.WriteLine(x);
}
}
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
public class Mister
{
int x = 11;
public void Bob()
{
int x = 10;
Console.WriteLine(this.x);
}
}
public class Mister
{
int x = 11;
public void Bob()
{
int x = 10;
Console.WriteLine(this.x);
}
}
"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
McMahone
McMahoneOP16mo ago
Ahhh so much easier now. Thank you So the instance variable of x would be 11, since we explicitly write this.x?
Joreyk ( IXLLEGACYIXL )
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
blinkbat
blinkbat16mo ago
is the property you're affecting named exactly as the param? casing and all
Joreyk ( IXLLEGACYIXL )
yes thats what is about
blinkbat
blinkbat16mo ago
yes exactly if you use capitalized properties you won't have this issue
Joreyk ( IXLLEGACYIXL )
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
blinkbat
blinkbat16mo ago
yes I avoid using this where I can
Joreyk ( IXLLEGACYIXL )
but its good to know as you will meet other shadowing problems in future
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server