C# auto implemented properties 🔐

preview_player
Показать описание
C# auto implemented properties tutorial example explained

#C# #auto-implemented #properties

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// Auto-Implemented properties = 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;
}
}
}
Рекомендации по теме
Комментарии
Автор

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.ReadKey();
}
}

class Car
{
public String Model {get; set;}

public Car(String model)
{
this.Model = model;
}
}
}

BroCodez
Автор

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.

prathameshgaikwad
Автор

Thank you for saving me time when i need to write my tests on a paper!!!

Ben-fnwp
Автор

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.

RebecaSantos-wtcz
Автор

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!

alysonwilliams
Автор

Very well organized playlists. Thank you

ademineshat
Автор

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.

fidelitycreate
Автор

Also, you can type "prop" and tap the "TAB" key twice, it will write everything for you so you don't have to remember it, you just change the name of the property.

FehAzeved
Автор

How come for the constructor, we used [ this.Model = model; ] whereas we only used [ Model = model; ] in the previous video of getters and setter? Is there any difference between the two?

reileypalma
Автор

Public Variable "Model" already can be access with dot . operator . Doesn't require get set, why add get set implementation

EagleEye
Автор

bro casually teaches programming for free

qwertzuwu