C
C#2y ago
♡ IV ♡

❔ properties/constructor question

class Car
{
public string model;
public string color;
public int year;

// Create a class constructor with multiple parameters
public Car(string modelName, string modelColor, int modelYear)
{
model = modelName;
color = modelColor;
year = modelYear;
}
class Car
{
public string model;
public string color;
public int year;

// Create a class constructor with multiple parameters
public Car(string modelName, string modelColor, int modelYear)
{
model = modelName;
color = modelColor;
year = modelYear;
}
Why do we need another variable (model = modelName) ? Why cant we just set it like this
class Car
{
public string model;
public string color;
public int year;

// Create a class constructor with multiple parameters
public Car(model, color, year)
{
}
}
class Car
{
public string model;
public string color;
public int year;

// Create a class constructor with multiple parameters
public Car(model, color, year)
{
}
}
Sorry for such a basic question 🙏
14 Replies
Angius
Angius2y ago
So, first things first, there are no properties in your code, just public fields And if you want something like that, you can use records Why do we need to assign the values explicitly? Well, what if you want to modify them somehow, or do some conditional logic?
♡ IV ♡
♡ IV ♡OP2y ago
oh i see, because then you would permanently alter the value instead of just for that object, right?
Angius
Angius2y ago
Not sure what you mean
class Car
{
public string Model { get; set; }
public string Color { get; set; }
public int Year { get; set; }

// Create a class constructor with multiple parameters
public Car(string modelName, string modelColor, int modelYear)
{
Model = modelName.ToUpper();
Color = modelColor.Trim().ToUpper();
Year = modelYear;
}
class Car
{
public string Model { get; set; }
public string Color { get; set; }
public int Year { get; set; }

// Create a class constructor with multiple parameters
public Car(string modelName, string modelColor, int modelYear)
{
Model = modelName.ToUpper();
Color = modelColor.Trim().ToUpper();
Year = modelYear;
}
Here's what I mean You might want to modify the values somewhat before setting the props
♡ IV ♡
♡ IV ♡OP2y ago
Is this considered setting?
Model = modelName.ToUpper();
Model = modelName.ToUpper();
Angius
Angius2y ago
Well, yeah
♡ IV ♡
♡ IV ♡OP2y ago
I also see the set; too, but just curious why we need it if we're setting the value in the constructor? Sorry ive been trying to wrap my head around this, tyvm
Angius
Angius2y ago
It sets the Model property { get; set; } is what makes a property A default getter and a default setter They can have more code in them, of course But the idea is to put a layer on top of the class's data. The data remains private (properties generate private fields on compile time) but the accessors are public Encapsulation and all that
♡ IV ♡
♡ IV ♡OP2y ago
right right
Angius
Angius2y ago
If you want the property to be settable just from the constructor, you can have just { get; }, no problem
♡ IV ♡
♡ IV ♡OP2y ago
Oh, wait so for this example, set; in this case is just an extra layer of protection, because we're setting the value in the constructor ?
Angius
Angius2y ago
set; allows the value to be set even outside of the constructor, from the outside of the class
var car = new Car(..., ..., ...);
car.Color = "red"; // invokes the setter
var car = new Car(..., ..., ...);
car.Color = "red"; // invokes the setter
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
♡ IV ♡
♡ IV ♡OP2y ago
thank you! I also didnt know what the => meant i kept seeing so thats hepful as well. tysm!
Angius
Angius2y ago
It's expression-bodied method The thing to the right of => has to be an expression or a one-liner statement
Accord
Accord2y 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