|
1 | | ---- |
| 1 | + --- |
2 | 2 | tags: |
3 | 3 | - oop |
4 | 4 | --- |
|
82 | 82 | - Properties are members of an object, just like instance variables and methods |
83 | 83 | - Access them with the "member access" operator, aka the dot operator |
84 | 84 | - For example, `myRect.Width` will access the property we wrote, assuming `myRect` is a Rectangle |
85 | | -- A complete example (available [as a complete solution](./code/projects/Properties_Example.zip)), where the "length" and "width" attributes are implemented with properties: |
| 85 | +- A complete example where the "length" and "width" attributes are implemented with properties: |
86 | 86 |
|
87 | | - ``` |
| 87 | + ```{download="./code/projects/Properties_Example.zip"} |
88 | 88 | !include code/projects/Properties_Example/Rectangle/Rectangle.cs |
89 | 89 | ``` |
90 | 90 |
|
@@ -159,14 +159,14 @@ tags: |
159 | 159 | ``` |
160 | 160 | - Note however that if either the set or get accessor is not the "trivial" one, then automatic properties cannot be used and the other accessor must be specified. |
161 | 161 | - For example, in the above code, simply writing `get;` instead of `get { return length; }` would give a compilation error. |
162 | | -- Note that properties can exist without backing field, and they can be *read-only* (that is, without a set accessor) or *write-only* (that is, without a get accessor, but this is rarer). |
| 162 | +- Note that properties can exist without backing field, and they can be *read-only* (that is, without a set accessor) or *write-only* (that is, without a get accessor, but this is more rare). |
163 | 163 | - An example of read-only property is as follows: |
164 | 164 |
|
165 | 165 | ``` |
166 | 166 | class Circle |
167 | 167 | { |
168 | 168 | public decimal Diameter { get; set; } |
169 | | - // The constructor below sets the value |
| 169 | + // The constructor below sets the value |
170 | 170 | // of the property's backing field through |
171 | 171 | // the property's set accessor. |
172 | 172 | public Circle(decimal dP) |
@@ -221,11 +221,11 @@ Putting together some of the elements discussed above, we can get for example th |
221 | 221 |
|
222 | 222 | ### Simple Notation |
223 | 223 |
|
224 | | -- Since properties represent (or, rather, allow to access) attributes, they go in the "attributes" box (the second box) |
225 | | -- If a property will simply "get" and "set" an instance variable of the same name, you do *not* need to write the instance variable in the box |
| 224 | +- Since properties represent (or, rather, allow to access) attributes, they go in the "attributes" box (the second box). |
| 225 | +- If a property will simply "get" and "set" an instance variable of the same name, you do *not* need to write the instance variable in the box, especially if those instance variables are not used anywhere other than through the property |
226 | 226 | - No need to write both the property `Width` and the instance variable `width` |
227 | | -- Syntax: `[+/-] «property» [name]: [type]` (where "«property»" is sometimes used in place of «property»). |
228 | | -- Note that the access modifier (+ or -) is for the property, not the instance variable, so it is + if the property is `public` (which it usually is) |
| 227 | +- Syntax: `[+/-] «property» [name]: [type]` (where "<<property>>" is sometimes used in place of «property»). |
| 228 | +- Note that the access modifier (+ or -) is for the property, not the instance variable, so it is + if the property is `public` (which it usually is). |
229 | 229 | - Example for `Rectangle`, assuming we converted both attributes to use properties instead of getters and setters: |
230 | 230 |
|
231 | 231 | !include diag/cla/Rectangle_with_properties.md |
|
0 commit comments