I’m an A level computer science student and I’m falling behind in my class because I don’t know c# as well as other students. Please can someone explain gets and sets and explain where and why they would be used
Imagine your a drug dealer and you don’t want police officers pretending to be users contacting you and having direct access to your operations location and all the information contained inside of it but you still want to be able to access your customers? (Attribute or field of a class)
What would you do?
You have a street dealer (getter ad setter) that users or undercover police officers! can contact to order products(get or set information) without ever having any knowledge of your operations.
The street dealers don’t know shit about anything they just know that they send an order and they get something back Which they give to the users.
This is really important for security because imagine if you have a nosy FBI agent investigate your code and notices you have a field called cocaineDrugPrice?
Maybe they can set your drug price to = 0 and buy everything you have for nothing and bankrupt your operation.
Sorry I’m not an expert 😂 I hope some of this makes sense
Wow out of all the analogies I’ve read this one has helped the most lol
I’m impressed
It is amusing and a little mixed up, but the principle behind it is OK. You said the street dealers were getters/setters, then you said the street dealers shouldn’t know anything. But your getter/setter does and must know about your internal implementation. It is the consumers of getters/setters that do not need to know anything–the users and police officers themselves. If you clean that up, it will work well.
Also maybe throw something in about being able to change the stash location whenever you want, and the users can still just go to their dealer. That is a main concern. Your getters/setters handle the details so consumers never have to worry about it. The details can change, and consumers don’t need to change with it.
In short, it helps in encapsulation. It make your values hidden from users.
Why to use property with get and send instead public values:
Better control of class members (reduce the possibility of yourself (or others) to mess up the code)
Fields can be made read-only (if you only use the get method), or write-only (if you only use the set method)
Flexible: the programmer can change one part of the code without affecting other parts
Increased security of data
It make your values hidden from users.
They’re not hidden from users if you can just get the value
You would use them to get and set values for private properties in your class. Is a way of encapsulating object properties so they can’t be changed from outside the object scope. Think of the bank account example. Would you want to be able to change the balance from everywhere in your code? If you have more bank accounts instantiated you might change the balance of them all.
https://www.freecodecamp.org/news/java-getters-and-setters/
Is the same in every language. In c# you can declare them as property{get; set;} I think…
yeah but what does that {get; set;} do? I know what it sets but what does it get? And yes you can
There are a lot of clear examples where getters and setters help. A common example is storing time – you may want to store seconds, minutes, and hours as separate integers. A setter function would allow you to set the time as a string, while the set function parsed it and set the individual fields. The get function would do the opposite.
But I think one of the most important aspects is often overlooked. Accessing your data through get/set functions allows you to change your implementation while keeping your interface the same. You can change an int to a float, while still maintaining get/set functions for ints, so your old code doesn’t break. You could even make drastic changes like replacing in-memory variables with database records, and have the get/set functions utilize the database instead, without any change to the consumers of your object. That’s not to say you should do anything like that, but the flexibility is there.
Also you can trace changes to property when debugging, meanwhile you can’t “detect changes” fo fields
Say what? Sounds like something I should be using.
what’s the difference between a property and a field
Since none of the other replies have shown any actual code, I’ll take a shot at it, but you really can learn everything you need to know (assuming you know the name of what it is you’re trying to learn) from the Microsoft documentation.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
But let’s start with some code. I’m going to use a single class called “MyClass”. The name is irrelevant. I’m just giving you a starting point.
Now let me break down what all the above is.
A class called “MyClass” that contains two public properties (SomeInteger
and SomeString
). They’re both accessible by any code that has created an instance of “MyClass” because they’re public
.
There is a private field called _stringBackingField
that is used internally and is not directly accessible from outside the class. This is not a property, but it’s used by the SomeString
property as the actual variable that stores the data.
The SomeInteger
property is using the “automatic property” style of declaration. It tells the compiler that we want a standard not-in-any-way-special property to be created. SomeInteger and SomeString are functionally identical to each other (except one for the data type, integer vs string).
The SomeString
property is using the more classical declaration style where the body of the getter and setter “methods” are explicitly declared. The value
keyword is a special placeholder for the setter to know what value your code wants to set the property to. In this case we’re doing NOTHING differently than the SomeInteger
code does. The only notable difference is that when we use this style of declaration, we have access to the backing field _stringBackingField
. There is actually a backing field for the SomeInteger
property too, but we don’t have easy access to it… this is by design.
Newer versions of C# allow the “arrow declaration” which replaces the body of whatever code it needs to replace. This is beyond the scope of your question here, but I thought I’d give it a mention because it’s part of the specification now.
Here is another “version” of “MyClass” which is identical in features to the one above, but uses the arrow declaration style.
The arrow declaration can replace the statements from our previous getter and setter body because they’re just single lines. If you want more information about this, see the documentation.
Variables get stored somewhere, and individiual methods may need to lookup(load) a Variable, to know what it is.
Those dependencies at the top of all c# methods, get and set plenty of things.
Try making a custom dependency, you will understand get and set by the end of it.
It is a standardized public interface that provides access to private variables in an Object.
From what I am reading I think you are more confused to how get and set work when declaring a property like this.
This is just a compiler shorthand that saves you from having to write out boilerplate code, this is called syntax sugar.
If you were to look at the code after it’s been desugared (yes that is a real computer science term) the code would look like this.
As you can see nothing fancy is happening. If you use the set, it sets the backing field, and if you use the get it gets the value.
Yeah someone put the same thing before you, but thank you!
C# devs
null reference exceptions