C
C#11mo 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
McMahone11mo 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
McMahone11mo 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
McMahone11mo 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
McMahone11mo 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
blinkbat11mo ago
is the property you're affecting named exactly as the param? casing and all
Joreyk ( IXLLEGACYIXL )
yes thats what is about
blinkbat
blinkbat11mo 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
blinkbat11mo 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
Accord11mo 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
More Posts
❔ BinaryFormatter.Serialize(Stream, object)' is obsolete, What do I use instead?I'm learning serialization in C# but it says binary formatters are obsolete. what should i use?❔ SingalR Hub Bearer Token AuthI am failing to see a simple way to add bearer token auth to a SignalR hub. The instructions on how ❔ DDD: How to apply a Domain specification on a Infrastructure EF Core?Hello everyone, I'm currently working on an app utilizing `DDD (Domain Driven Design)` and `Microse❔ WPF Change ViewsIm trying to switch views with MVVM pattern❔ Executing a method every n seconds but only during certain conditionsWhat would be the best way to do this? The easiest way I can think of would be: ```cs Task.Run(MainA❔ GtkSharp: Creating a GClosureSimply put, I am trying to complete the following code: ```csharp AccelGroup binds = new AccelGroup(✅ Confused by constant errors regarding class extension.Hi all, for my uni course, I need to code a game. I have chosen TicTacToe, but it needs to use diffe❔ How would you embed a Git commit hash in an assembly?I want to stamp a commit hash into a binary so I can display it in the UI. I'm fine with pulling stu❔ Is it possible to run integration tests with typescript as client and .net api as server?My hope is to test a client written in typescript which calls a .net api in the same solution.✅ .csproj – how to generate nodes dynamically by iterating over a string array ?how can i generate xml code in the .csproj file? i dont want to repeat my actions 10000 times... so