C# getters & setters 🔒

  Рет қаралды 137,044

Bro Code

Bro Code

2 жыл бұрын

C# getters and setters encapsulation tutorial example explained
#C# #getters #setters
//getters & setters = add security to fields by encapsulation
// They're accessors found within properties
// properties = combine aspects of both fields and methods (share name with a field)
// get accessor = used to return the property value
// set accessor = used to assign a new value
// value keyword = defines the value being assigned by the set (parameter)

Пікірлер: 82
@BroCodez
@BroCodez 3 жыл бұрын
using System; namespace MyFirstProgram { class Program { static void Main(string[] args) { //getters & setters = add security to fields by encapsulation // They're accessors found within properties // properties = combine aspects of both fields and methods (share name with a field) // get accessor = used to return the property value // set accessor = used to assign a new value // value keyword = defines the value being assigned by the set (parameter) Car car = new Car(400); car.Speed = 1000000000; Console.WriteLine(car.Speed); Console.ReadKey(); } } class Car { private int speed; public Car(int speed) { Speed = speed; } public int Speed { get { return speed; } set { if (value > 500) { speed = 500; } else { speed = value; } } } } }
@reignydaphne
@reignydaphne 2 жыл бұрын
I tried to run this code but it shows value hasn't been declared! Can you please explain how did you run it?
@lifeinloop
@lifeinloop 2 жыл бұрын
@@reignydaphne value is a keyword that exists inside every set and represents the value that is received.
@learnersmail9944
@learnersmail9944 2 жыл бұрын
thank you bro
@scubagoblin
@scubagoblin 7 ай бұрын
I just read 15 pages over this concept and walked away more confused than before I started. You explain concepts in such a beginner-friendly way and don't include a lot of random unnecessary stuff. I really appreciate you and the content you make. I feel like a lot of programmers have been coding for so long that they forget to slow down a bit when teaching people.
@brunopereira4643
@brunopereira4643 7 ай бұрын
Yea, i understand now, like... now i know what is an namespace, what is a class, methods, etc etc... , but i remember when i started and ask: "TF is that", so we need to be humble and put ourselves in the place of those who are starting.
@urmomlolgottem
@urmomlolgottem 5 ай бұрын
trying to find tutorials on things not covered by brocode is literally hell good luck out there
@lezocsfn1979
@lezocsfn1979 5 ай бұрын
to understand a little more about it, watch the video of Brackeys on c# properties
@DrN0VA.
@DrN0VA. 9 ай бұрын
This really helped me understand. It seems like getters and setters can be described as: Do this when someone GETS the variable. Do this when someone SETS the variable.
@TripalYT
@TripalYT Жыл бұрын
After all these years of coding and never understanding any of this. You helped me understand it within 3 minutes. Thank you!
@davidesdras45
@davidesdras45 2 жыл бұрын
Man, I always had so much diffilcult with this. But you, you bro, you in a 4 minutes video you managed to explain to me masterfully. You're a legend bro. You just got a new subscribe and fan!
@binjozoken6055
@binjozoken6055 2 жыл бұрын
That was a brilliant way of explaining C# properties. Thanks. I'm sure I'll be back to watch it again.
@FunnySubmarine-ij4zk
@FunnySubmarine-ij4zk 2 ай бұрын
Thank youu. It feels like it's the first time to actually understand what getters & setters are and why we use them.
@antoinenowakowski6181
@antoinenowakowski6181 Жыл бұрын
hello i'm french and we can understand you very well, thanks.
@32KZ
@32KZ Жыл бұрын
Explaine better than all my teachers combind. thank you for actually letting this make sense.
@Rahulsingh-theraha
@Rahulsingh-theraha 2 жыл бұрын
Awesome explanation..I was so confused on its use case...But u made me understand completely ☺️
@naruto8170
@naruto8170 Жыл бұрын
Just went through a 2 hour C# course, and it ended right after he showed us classes, fields, and constructors. I don’t think properties and getters/setters were even mentioned.. So, going along with another tutorial, I was very perplexed when he put in { get; set; }. I tried to read the documentation online, but just confused myself even more. Your 4 minute video explained it well enough for me to completely understand it, so thank you!!
@amjadhani8929
@amjadhani8929 11 ай бұрын
Wow. I understood getters and setters in only 4 minutes. What a great illustration video. Thanks!
@stevancosovic4706
@stevancosovic4706 2 жыл бұрын
Thanks bro, very good explanation, this channel deserves more views and support!
@Slaygee
@Slaygee Жыл бұрын
Thank you, this was the best explanation I've seen.
@Ally-uj3di
@Ally-uj3di Жыл бұрын
You have no idea how hard I cried: ""OOOOOOHHHHH!"" After this tutorial. It all makes sense now! THANK YOU!!! I have been asking ChatGPT to explain me this, and it was failing miserably :')
@GothicRK
@GothicRK Жыл бұрын
Wow I FINALLY understood this. Thank you!!
@belajashark9211
@belajashark9211 Жыл бұрын
Thank you! Had to watch it 4 times, but now almost understand cause need this)))
@e-tradingbd8421
@e-tradingbd8421 2 жыл бұрын
nice understanding capabilities !!! commendable
@user-mf8nu5qe5f
@user-mf8nu5qe5f Жыл бұрын
Hello Bro Code, your vids are always helpful. Could you perhaps do tutorials on C# lambdas and LINQ I feel like you are the only one that could explain them so I can understand thanks
@GalaxyCookies76
@GalaxyCookies76 Жыл бұрын
Simple explaination, Good Job
@ofaydn
@ofaydn 2 жыл бұрын
thanks for explaining this topic so simple
@kroepkroep
@kroepkroep Жыл бұрын
Thank you for the explenation. I guess that the security talked about in this video is about what the user can do? Via the front end?
@gabrielbarrett9784
@gabrielbarrett9784 Жыл бұрын
Thanks for making it easier to understand
@bamdad4927
@bamdad4927 Жыл бұрын
Thanks bro Very cool 👍🏻
@isaacurban2790
@isaacurban2790 Жыл бұрын
You make things so easy
@spartanranger
@spartanranger 2 жыл бұрын
Thanks for the video Bro.
@30hi31
@30hi31 Жыл бұрын
Chulada tu video hermanó Gretting from Mexico
@irissupercoolsy
@irissupercoolsy Жыл бұрын
Thank you! Coming from Java, the getters and setters were a bit confusing in C# at first
@alberttoo5807
@alberttoo5807 2 ай бұрын
I think you could still use the java way or use properties
@noahyannis2465
@noahyannis2465 2 жыл бұрын
Then why do we use getters and setters in properties if there is no validation/additional code in most setters?
@user-nv4qw4cv6w
@user-nv4qw4cv6w Ай бұрын
Very good video!
@eliaselmokadem2922
@eliaselmokadem2922 Жыл бұрын
its unbelievable how many times this guy saved my ass from an exam xd
@Nick-qg5bt
@Nick-qg5bt Жыл бұрын
Thank you so much. you saved me
@MHX11
@MHX11 Жыл бұрын
That's an amazing example, thanks so much, you saved a lot of time
@IntergalacticWither
@IntergalacticWither 2 ай бұрын
this is such a good video tysm
@elmer5455
@elmer5455 Жыл бұрын
thanks always had problems with this.
@letuslearn8433
@letuslearn8433 Жыл бұрын
It is interesting video.keep it up mr
@codeme8016
@codeme8016 2 ай бұрын
Perfect!
@meghpatel2219
@meghpatel2219 2 жыл бұрын
Bro please some C# projects, something like you have done in python playlist 🙏
@amplifiedge
@amplifiedge Жыл бұрын
Thank you Bro!
@windows99
@windows99 2 жыл бұрын
I don't understand the difference. If we wanted to limit the editing of the field "speed" by making it private, aren't we allowing it with the set property? In the end, we are allowing editing of speed. I'm a complete beginner, just to note.
@cranebird1467
@cranebird1467 Жыл бұрын
set&get are methods so you can create only get without set in this case field become read-only.
@orvobx7909
@orvobx7909 Жыл бұрын
You are allowing it tk be changed, but you are setting parameters for how it can be changed
2 жыл бұрын
Thanks Bro!
@diandradeeke
@diandradeeke 6 ай бұрын
but if you create a unique getter and setter and only programm that the value is set to the value the function gets or returns the current value, than its not different than without using getters and setters, isnt it?
@user-yz5vq1yb8o
@user-yz5vq1yb8o Жыл бұрын
Love you bro
@Cruz0e
@Cruz0e 2 жыл бұрын
example to my other comment, I just seen something like this private int _progress; public int progress { get { return rank == 8 ? 0 : _progress; } set { rank += value / 100; _progress = value % 100; } }
@shotgunflamez5608
@shotgunflamez5608 Жыл бұрын
2:57 no need for the else statement because if the "if" statement fails it'll just skip it and execute any code that is after the statement. Other than that pretty cool video
@easternunit2009
@easternunit2009 Жыл бұрын
well for this example, he wants to make sure that the speed doesn’t go over 500 right?
@user-mu3bh4nt3b
@user-mu3bh4nt3b 2 жыл бұрын
coooool
@Cruz0e
@Cruz0e 2 жыл бұрын
I am not sure why my code/comment disappeared so i write again, but I just wanted to note the private variable + getter/setter does not have to be the same name like private speed and public Speed... I just seen a code where the variable was called _rank and rank, so it is just about your coding style
@Cruz0e
@Cruz0e 2 жыл бұрын
so they do not have to share the same name
@mrtaluspa9903
@mrtaluspa9903 Жыл бұрын
thxxx😍
@o9o947
@o9o947 Жыл бұрын
all good and great but how do u call the getter in the main method? u just showed how to call set
@pieteb_nl
@pieteb_nl 2 жыл бұрын
Puzzeled you can just use 'speed' in the setter, without defining it as a method parameter. This wouldn't be possible in java.
@bhadriramlella7697
@bhadriramlella7697 Жыл бұрын
How can I take an input in the main function using getters and setters is there any way? If there is please explain me.
@bhadriramlella7697
@bhadriramlella7697 Жыл бұрын
I mean using cin in the main function with getters and setters
@danielbertrand8796
@danielbertrand8796 8 ай бұрын
what a bro
@ciprisad
@ciprisad 6 ай бұрын
when you make the car object and give its speed you still can make the speed greater than 500, i would like to know how to limit this as well eg: Car car1 = new Car(500000);
@TwitchCronos100
@TwitchCronos100 5 ай бұрын
Put the same logic as your setter in your constructor where you set Speed = speed
@kdenisinfo
@kdenisinfo 2 жыл бұрын
Do we need private field per each property? Or not
@sok8557
@sok8557 2 жыл бұрын
properties should be public while the fields the properties access should be private (doesnt always have to be, but if you want to apply encapsulation to the fields and use the get set accessors then properties should be public while fields should be private)
@sok8557
@sok8557 2 жыл бұрын
honestly rambled on a lot but tdlr: property: public while field = private (the reason for the property)
@maxyan2572
@maxyan2572 Жыл бұрын
noice
@vimukthikulasekara4724
@vimukthikulasekara4724 2 ай бұрын
chad bro
@jirikunt1457
@jirikunt1457 11 ай бұрын
My C# teacher would hate you for declaring get set like that xD
@edinetgrunhed6000
@edinetgrunhed6000 Жыл бұрын
what is '400' in this code ? Car car = new Car(400); how to display that 400?
@whitedinamo
@whitedinamo 2 жыл бұрын
lesson check😇
@McPatoo
@McPatoo 7 ай бұрын
Another1 :))))🤑
@Saskamon
@Saskamon 2 жыл бұрын
Getters and Setters sounds like Jeepers Creepers to me.
@triton5457
@triton5457 2 жыл бұрын
burger 🍔
@Lonchanick
@Lonchanick Жыл бұрын
Field name = PascalCase propertyName = camelCase
@davidesoltys8411
@davidesoltys8411 3 ай бұрын
isnt better to call the "private int speed" to "private int _speed" ???
@drake_eric9799
@drake_eric9799 Жыл бұрын
How to use this in string In field get set
@drake_eric9799
@drake_eric9799 Жыл бұрын
Now I know I always mistake in curly braceses
C# auto implemented properties 🔐
2:46
Bro Code
Рет қаралды 38 М.
C# generics ⁉️
5:33
Bro Code
Рет қаралды 66 М.
How many pencils can hold me up?
00:40
A4
Рет қаралды 18 МЛН
Тяжелые будни жены
00:46
К-Media
Рет қаралды 5 МЛН
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 12 МЛН
ELE QUEBROU A TAÇA DE FUTEBOL
00:45
Matheus Kriwat
Рет қаралды 27 МЛН
This is the best way to learn C++ for free
0:40
Mehul - Codedamn
Рет қаралды 343 М.
Getters & Setters | C# | Tutorial 28
12:09
Giraffe Academy
Рет қаралды 189 М.
C# static 🚫
5:03
Bro Code
Рет қаралды 62 М.
Fundamental Concepts of Object Oriented Programming
9:16
Computer Science
Рет қаралды 843 М.
C# interfaces 🐟
5:38
Bro Code
Рет қаралды 117 М.
C# Tutorial: Properties, Getters and Setters
11:39
Ian Schoenrock
Рет қаралды 25 М.
Getters & Setters | C++ | Tutorial 30
12:41
Giraffe Academy
Рет қаралды 63 М.
Java Tutorial #8: Getters and Setters Explained
8:24
CodingWithChandler
Рет қаралды 100 М.
Не обзор DJI Osmo Pocket 3 Creator Combo
1:00
superfirsthero
Рет қаралды 1,3 МЛН
5 НЕЛЕГАЛЬНЫХ гаджетов, за которые вас посадят
0:59
Кибер Андерсон
Рет қаралды 1,1 МЛН