Can somebody help me understand the get
& set
?
Why are they needed? I can just make a public variable.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Get and set are used in properties. They can each be public, protected, or private. Similar to accessor and mutator methods, they allow some computation when code tries to access/mutate the property. Of course, as long as you define one of get/set, the other is optional.
Example without properties:
With properties:
They are called Accessors
http://msdn.microsoft.com/en-us/library/w86s7x04.aspx
Then in the incarnation of C# 3, you can do this much easier through auto-properties
http://msdn.microsoft.com/en-us/library/bb384054.aspx
Simply put,
get
andset
accessors are the functions called on a Property; that is, when you retrieve the value or when you set it. It forces a type of behavior on the way values are retrieved or set.For example, you may want to have a mechanism to get/set passwords. Generally speaking, you'll only want to compare the hash of a password instead of storing things plaintext, so you'd have the getter variable retrieve the stored hash, and the setter would take the provided input and hash it for storage.
Here's what I mean:
value
is the variable provided to the setter from what has been given in the variable assignment. e.g.:someuser.Password = "blah";
Properties act as accessors to the internal state of an object, hiding the implementation of that state.
So, for example, you may have a first name property in a class
So anyone using the class doesn't need to know how first name is stored, they just know they can get a string representation of it. By adding a set you also add a mutator, something which changes an objects internal state
Again you're still isolating how the first name is stored internally (encapsulation), but users can change it by passing in a string.
get{} and set{} are accessors that offer up the ability to easily read and write to private fields. Working with a simple example:
In this case, Bar is a public property that has a getter and a setter that allows access to the private field _bar that would otherwise be inaccessible beyond class Foo.
Now in a class that has an instace of Foo, you can do this:
So the public accessor allows you to set the value of the private field back in Foo.
Hope that helps!
Warning: I am assuming you already know about object-oriented programming.
What are properties?
Properties are language elements that allow you to avoid the repetitive
getXYZ()
accessors andsetXYZ()
mutators techniques found in other languages, like Java.Why do they exist?
They aim to solve the following problems:
Saying
get
andset
in the beginning of every access or mutation of a value is annoying and distracting.In Java, you often say:
and then consistently say:
After a while, the
get
andset
become rather annoying.Providing direct access to the actual variable breaks encapsulation, so that's not an option.
How are they used?
They are used just like variables. You read/write to them just like variables.
How are they created?
They are created as methods. You define a pair of methods that:
Return the current value of the property. Oftentimes, this is nothing more than something like the following:
Set the value of the property:
Other notes:
Auto-implemented Properties
C# 3.0 introduced auto-implemented properties:
This is equivalent to:
Why does it exist?
It helps you avoiding breaking changes in client executables.
Let's say you're lazy and don't want to type the whole thing, and decide to expose a variable publicly. You then create an executable that reads from or writes to that field. Then you change your mind and decide that you in fact needed a property, so you change it to one.
What happens?
The depending executable breaks, because the code is no longer valid.
Auto-implemented properties help you avoid that, without extra redundancy in your initial code.
Indexers
Indexers extend the property syntax to let you index objects (surprise!), just like arrays.
For C++ users: This is similar to overloading
operator []
.Example:
You then use them like
obj[5] = 10;
, which is equivalent to calling theset
method ofobj
's indexer.In fact,
System.Collections.Generic.List<T>
is indexed:Isn't that neat? :)
Anything else?
There are many more features to properties, not all of which are available in C#: