mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-05 18:36:41 +00:00
Fix record class & record struct notes
This commit is contained in:
parent
8d80356d2b
commit
20d9d6deb9
1 changed files with 19 additions and 12 deletions
|
@ -320,22 +320,29 @@ Since the names of the attributes of a tuple do not matter (a tuple is an instan
|
|||
## Records (C# 9)
|
||||
|
||||
```cs
|
||||
// mutable record
|
||||
public record RecordName
|
||||
public record Record
|
||||
{
|
||||
public Type Property1 { get; init; }
|
||||
public Type Property2 { get; init; }
|
||||
|
||||
// Equals(), Equality Operators, GetHashCode(), ToString() generated by .NET
|
||||
// Constructor, Deconstructor, Equals(), Equality Operators, GetHashCode(), ToString() generated
|
||||
}
|
||||
// same as
|
||||
public record Record(Type Property1, Type Property2, ...);
|
||||
public record struct RecordStruct(Type Property1, Type Property2, ...);
|
||||
|
||||
// immutable record
|
||||
public record RecordName ( Type Property1, Type Property2, ...);
|
||||
// Constructor, Deconstructor, Equals(), Equality Operators, GetHashCode(), ToString() generated by .NET
|
||||
|
||||
RecordName record = new RecordName(value1, ...); // instantiation
|
||||
Record recObj = new Record(value1, ...); // instantiation
|
||||
//or
|
||||
RecordName record = new RecordName { Property1 = value1, ...}; // instantiation
|
||||
Record record = new Record { Property1 = value1, ...}; // instantiation
|
||||
```
|
||||
|
||||
### Custom Behaviour
|
||||
|
||||
```cs
|
||||
public record Record(Type Property1, Type Property2, ...)
|
||||
{
|
||||
// provide custom implementation for default maethod
|
||||
}
|
||||
```
|
||||
|
||||
### `with`-expressions
|
||||
|
@ -352,7 +359,7 @@ A record implicitly defines a protected "copy constructor", a constructor that t
|
|||
The `with` expression causes the copy constructor to get called, and then applies the object initializer on top to change the properties accordingly.
|
||||
|
||||
```cs
|
||||
protected RecordName(RecordName original) { /* copy all the fields */ } // generated
|
||||
protected Record(Record original) { /* copy all the fields */ } // generated
|
||||
```
|
||||
|
||||
**NOTE**: it's possible to define a custom copy constructor tha will be picked up by the `with` expression.
|
||||
|
@ -373,8 +380,8 @@ A with-expression simply calls the hidden "clone" method and applies the object
|
|||
public record Base{ Type Prop1, Type Prop2 };
|
||||
public record Derived : Base { Type Prop3 };
|
||||
|
||||
Base _base = new Derived { Prop1 = value1, Prop2 = value2, Prop3 = value3 };
|
||||
newBase = _base with { Prop2 = value }; // new Derived record even if type of _base is Base since _base contains a Derived record
|
||||
Base @base = new Derived { Prop1 = value1, Prop2 = value2, Prop3 = value3 };
|
||||
var newBase = @base with { Prop2 = value }; // new Derived record even if type of _base is Base since _base contains a Derived record
|
||||
```
|
||||
|
||||
### Value-based Equality & Inheritance
|
||||
|
|
Loading…
Add table
Reference in a new issue