Комментарии:
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// auto-Implemented property = shortcut when no additional logic is required in the property
// you do not have to define a field for a property,
// you only have to write get; and/or set; inside the property
Car car = new Car("Porsche");
Console.WriteLine(car.Model);
Console.ReadKey();
}
}
class Car
{
public String Model {get; set;}
public Car(String model)
{
this.Model = model;
}
}
}
bro casually teaches programming for free
Ответитьgoat code
ОтветитьPublic Variable "Model" already can be access with dot . operator . Doesn't require get set, why add get set implementation
ОтветитьThank you for saving me time when i need to write my tests on a paper!!!
ОтветитьIt's amazing how easy i could understand this hahahaha i was having such a hard time to understand some c# stuff and his channel really helped.
ОтветитьThanks
ОтветитьHelpful
ОтветитьRandom
ОтветитьReeeeeee
Ответитьthanks bro
ОтветитьWhy can't we simply have a public variable then. We might also avoid writing get and set. Can you please explain why auto implementation is preferred over normal public instance variable.
ОтветитьThanks!
Ответитьwhy not just setting Model as a public field?
class Car {
public String Model;
}
Hi, I've tried using auto properties to make a read only vairable outside of the class. But when I do, any function within the class is now saying that property is read only... But we're still inside the class??? This seems stupid to me. But I got around it by using regular properties.
[EDIT]
I just learned how to make a read-only auto property and have it still editable inside the class :
public float var { get; private set; } = 0f;
The key was "private set".
Apparently, if you use just "{ get; }" you can only modify the property once in the class' constructor.
taco 🌮
Ответитьlesson check😇
Ответитьvery helpful! i got so confused for a second because I was thinking you were going to define the model somewhere else too lol porsche is technically the make, not the model. Anyway, this vid helped a lot!
ОтветитьThanks for the video Bro.
Ответитьhello BRO BIG FAN FROM INDIA
Ответить