C# getters & setters 🔒

  Рет қаралды 171,386

Bro Code

Bro Code

Күн бұрын

Пікірлер: 87
@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 3 жыл бұрын
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 3 жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
trying to find tutorials on things not covered by brocode is literally hell good luck out there
@lezocsfn1979
@lezocsfn1979 Жыл бұрын
to understand a little more about it, watch the video of Brackeys on c# properties
@DrN0VA.
@DrN0VA. Жыл бұрын
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 2 жыл бұрын
After all these years of coding and never understanding any of this. You helped me understand it within 3 minutes. Thank you!
@davidesdras45
@davidesdras45 3 жыл бұрын
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!
@Jordan-qi2dn
@Jordan-qi2dn 7 ай бұрын
If anyone else didn't understand at first I'll try to explain my interpretation. The point of getters/setters is so that we can sort of limit/encapsulate information by having a private field, and we have our property sort of act as a decryption key, I don't really know a good analogy for setters/getters. I think we can make things read only if we private the setter though. The get accessor is used when we're reading/accessing the value of a property. ie. Console.WriteLine(Class.Property); The set accessor is used when we're writing/reassigning the value of a property ie. Class.Property = 550; The value keyword comes from what we input in the constructor. You can only access either set/get at a time as you're either reading/writing, and you can put your own logic/conditonal statements in there like bro did with the if/else statements. In the write example. when we're instantiating a new car Object, and we input 550 as an argument, we are writing/reassigning. Car car_1 = new Car(550); Here we're writing, int value of 550 is in the constructor, therefore we're going to use the set accessor. If our value (550) is greater than 500, it sets the speed to max 500, otherwise our speed will just be set to whatever we inputted as long as it's
@binjozoken6055
@binjozoken6055 3 жыл бұрын
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 11 ай бұрын
Thank youu. It feels like it's the first time to actually understand what getters & setters are and why we use them.
@cactusdoodle8619
@cactusdoodle8619 5 ай бұрын
Bro, thankyou. I’ve been binging the entire c# series, I’m getting to the real meat and potatoes of my summer project. Properties were so hard to understand through my college course. You’re a great teacher.
@32KZ
@32KZ Жыл бұрын
Explaine better than all my teachers combind. thank you for actually letting this make sense.
@JustBen
@JustBen 6 ай бұрын
Bro, you just explained a concept that college could not! Glad I found this channel.
@antoinenowakowski6181
@antoinenowakowski6181 2 жыл бұрын
hello i'm french and we can understand you very well, thanks.
@amjadhani8929
@amjadhani8929 Жыл бұрын
Wow. I understood getters and setters in only 4 minutes. What a great illustration video. Thanks!
@Rahulsingh-theraha
@Rahulsingh-theraha 3 жыл бұрын
Awesome explanation..I was so confused on its use case...But u made me understand completely ☺️
@san_dingus
@san_dingus Жыл бұрын
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!!
@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 :')
@Slaygee
@Slaygee 2 жыл бұрын
Thank you, this was the best explanation I've seen.
@GalaxyCookies76
@GalaxyCookies76 Жыл бұрын
Simple explaination, Good Job
@stevancosovic4706
@stevancosovic4706 3 жыл бұрын
Thanks bro, very good explanation, this channel deserves more views and support!
@diandradeeke
@diandradeeke Жыл бұрын
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?
@letuslearn8433
@letuslearn8433 2 жыл бұрын
It is interesting video.keep it up mr
@eliaselmokadem2922
@eliaselmokadem2922 Жыл бұрын
its unbelievable how many times this guy saved my ass from an exam xd
@belajashark
@belajashark Жыл бұрын
Thank you! Had to watch it 4 times, but now almost understand cause need this)))
@e-tradingbd8421
@e-tradingbd8421 2 жыл бұрын
nice understanding capabilities !!! commendable
@bamdad4927
@bamdad4927 2 жыл бұрын
Thanks bro Very cool 👍🏻
@gabrielbarrett9784
@gabrielbarrett9784 Жыл бұрын
Thanks for making it easier to understand
@30hi31
@30hi31 2 жыл бұрын
Chulada tu video hermanó Gretting from Mexico
@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?
@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
@GothicRK
@GothicRK 2 жыл бұрын
Wow I FINALLY understood this. Thank you!!
@GuilhermeFrank-y6j
@GuilhermeFrank-y6j 9 ай бұрын
Very good video!
@ofaydn
@ofaydn 2 жыл бұрын
thanks for explaining this topic so simple
@MHX11
@MHX11 2 жыл бұрын
That's an amazing example, thanks so much, you saved a lot of time
@irissupercoolsy
@irissupercoolsy Жыл бұрын
Thank you! Coming from Java, the getters and setters were a bit confusing in C# at first
@alberttoo5807
@alberttoo5807 10 ай бұрын
I think you could still use the java way or use properties
@isaacurban2790
@isaacurban2790 2 жыл бұрын
You make things so easy
@IntergalacticWither
@IntergalacticWither 10 ай бұрын
this is such a good video tysm
@spartanranger
@spartanranger 3 жыл бұрын
Thanks for the video Bro.
@noahyannis2465
@noahyannis2465 2 жыл бұрын
Then why do we use getters and setters in properties if there is no validation/additional code in most setters?
@elmer5455
@elmer5455 2 жыл бұрын
thanks always had problems with this.
@danielbertrand8796
@danielbertrand8796 Жыл бұрын
what a bro
@meghpatel2219
@meghpatel2219 3 жыл бұрын
Bro please some C# projects, something like you have done in python playlist 🙏
@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 2 жыл бұрын
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
@jimbobthedog
@jimbobthedog 2 ай бұрын
It's allowing you to add more code to updating the value of speed. He explains it at the end. If you use the setter, you can put code that limits the size of the value someone tries to set.
@shotgunflamez5608
@shotgunflamez5608 2 жыл бұрын
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 2 жыл бұрын
well for this example, he wants to make sure that the speed doesn’t go over 500 right?
@amplifiedge
@amplifiedge Жыл бұрын
Thank you Bro!
@ciprisad
@ciprisad Жыл бұрын
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);
@Ripcraze
@Ripcraze Жыл бұрын
Put the same logic as your setter in your constructor where you set Speed = speed
@Jordan-qi2dn
@Jordan-qi2dn 7 ай бұрын
Your setter should have the condition below, if the value within your constructor is greater than 500, the if statement will cap it at 500, otherwise it accepts the value and initializes speed to it. if(value>500) { speed = 500 } else { speed = value }
@MichaelS-u6g
@MichaelS-u6g Жыл бұрын
Love you bro
@o9o947
@o9o947 2 жыл бұрын
all good and great but how do u call the getter in the main method? u just showed how to call set
@codeme8016
@codeme8016 11 ай бұрын
Perfect!
@Cruz0e
@Cruz0e 3 жыл бұрын
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; } }
@kdenisinfo
@kdenisinfo 3 жыл бұрын
Do we need private field per each property? Or not
@sok8557
@sok8557 3 жыл бұрын
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 3 жыл бұрын
honestly rambled on a lot but tdlr: property: public while field = private (the reason for the property)
3 жыл бұрын
Thanks Bro!
@bhadriramlella7697
@bhadriramlella7697 2 жыл бұрын
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 2 жыл бұрын
I mean using cin in the main function with getters and setters
@mrvladimirlenin
@mrvladimirlenin 2 жыл бұрын
thxxx😍
@Cruz0e
@Cruz0e 3 жыл бұрын
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 3 жыл бұрын
so they do not have to share the same name
@davidesoltys8411
@davidesoltys8411 11 ай бұрын
isnt better to call the "private int speed" to "private int _speed" ???
@scarletdreams6689
@scarletdreams6689 Ай бұрын
thanks dad
@pieteb_nl
@pieteb_nl 3 жыл бұрын
Puzzeled you can just use 'speed' in the setter, without defining it as a method parameter. This wouldn't be possible in java.
@edinetgrunhed6000
@edinetgrunhed6000 Жыл бұрын
what is '400' in this code ? Car car = new Car(400); how to display that 400?
@jirikunt1457
@jirikunt1457 Жыл бұрын
My C# teacher would hate you for declaring get set like that xD
@McPatoo
@McPatoo Жыл бұрын
Another1 :))))🤑
@Lonchanick
@Lonchanick Жыл бұрын
Field name = PascalCase propertyName = camelCase
@김-y8b1s
@김-y8b1s 3 жыл бұрын
coooool
@vimukthikulasekara4724
@vimukthikulasekara4724 10 ай бұрын
chad bro
@whitedinamo
@whitedinamo 3 жыл бұрын
lesson check😇
@Saskamon
@Saskamon 3 жыл бұрын
Getters and Setters sounds like Jeepers Creepers to me.
@maxyan2572
@maxyan2572 Жыл бұрын
noice
@triton5457
@triton5457 2 жыл бұрын
burger 🍔
@drake_eric9799
@drake_eric9799 2 жыл бұрын
How to use this in string In field get set
@drake_eric9799
@drake_eric9799 2 жыл бұрын
Now I know I always mistake in curly braceses
C# auto implemented properties 🔐
2:46
Bro Code
Рет қаралды 48 М.
Getters & Setters | C# | Tutorial 28
12:09
Giraffe Academy
Рет қаралды 197 М.
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
C# generics ⁉️
5:33
Bro Code
Рет қаралды 85 М.
C# interfaces 🐟
5:38
Bro Code
Рет қаралды 149 М.
Getters and Setters Java Tutorial #84
14:12
Alex Lee
Рет қаралды 515 М.
C# polymorphism 🎭
5:11
Bro Code
Рет қаралды 92 М.
"Stop Using Properties in C#, Just Use Fields" | Code Cop #013
11:53
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 1 МЛН
Getters & Setters | C++ | Tutorial 30
12:41
Giraffe Academy
Рет қаралды 67 М.
Every single feature of C# in 10 minutes
9:50
Train To Code
Рет қаралды 175 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 682 М.