mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-07 03:16:41 +00:00
Corrections to Initializers and Setters notes
This commit is contained in:
parent
2619576191
commit
a2c9836e1c
1 changed files with 9 additions and 23 deletions
|
@ -1440,7 +1440,10 @@ Class obj = new(arg1, ...)
|
||||||
|
|
||||||
// object initializers
|
// object initializers
|
||||||
Class obj = new Class { Prop2 = arg2, Prop1 = arg1 }; // w/o constructor
|
Class obj = new Class { Prop2 = arg2, Prop1 = arg1 }; // w/o constructor
|
||||||
Cat sameCat = new Cat(arg1){ Prop2 = arg2 }; // w/ constructor
|
Class obj = new Class(arg1) { Prop2 = arg2 }; // w/ constructor
|
||||||
|
|
||||||
|
// with keyword
|
||||||
|
var copy = original with { Prop = newValue }; // other props are copies of the original
|
||||||
```
|
```
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
|
@ -1544,10 +1547,11 @@ class Class
|
||||||
// EXPRESSION-BODIED READ-ONLY PROPERTY
|
// EXPRESSION-BODIED READ-ONLY PROPERTY
|
||||||
public type Field => <expr>;
|
public type Field => <expr>;
|
||||||
|
|
||||||
// AUTO-PROPERTIES (cannot be write only)
|
// AUTO-PROPERTIES
|
||||||
public Property { get; set; }
|
public Property { get; set; }
|
||||||
public Property { get; private set; }
|
public Property { get; private set; } // settable only inside class
|
||||||
public Property { get; } // effectively read-only ever without keyword, can only be setted by constructor
|
public Property { get; init; } // settable only in constructor, initilizer, keyword with
|
||||||
|
public Property { get; } // can only be setted by constructor
|
||||||
|
|
||||||
// MIXED
|
// MIXED
|
||||||
public type Property
|
public type Property
|
||||||
|
@ -1561,27 +1565,9 @@ class Class
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Init-Only Properties
|
**NOTE**: The `init` accessor is a variant of the `set` accessor which can only be called during object initialization.
|
||||||
|
|
||||||
The `init` accessor is a variant of the `set` accessor which can only be called during object initialization.
|
|
||||||
|
|
||||||
Because `init` accessors can only be called during initialization, they are allowed to *mutate* `readonly` fields of the enclosing class, just like in a constructor.
|
Because `init` accessors can only be called during initialization, they are allowed to *mutate* `readonly` fields of the enclosing class, just like in a constructor.
|
||||||
|
|
||||||
```cs
|
|
||||||
public class ClassName
|
|
||||||
{
|
|
||||||
private readonly Type field;
|
|
||||||
|
|
||||||
public Type Property
|
|
||||||
{
|
|
||||||
get => field;
|
|
||||||
init => field (value ?? throw new ArgumentNullException(nameof(field)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type Property { get; init; }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Indexers
|
### Indexers
|
||||||
|
|
||||||
An **indexer** is a property that takes one or more arguments, and is accessed with the same syntax as is used for arrays.
|
An **indexer** is a property that takes one or more arguments, and is accessed with the same syntax as is used for arrays.
|
||||||
|
|
Loading…
Add table
Reference in a new issue