3.1.13 The nameof operator

Jason Zhu 2021-08-09 21:44:10 +10:00
parent 85ef515f55
commit 0685f1423a

@ -309,7 +309,67 @@ class Class1
Details of Finalizer is in Chap12
### 3.1.12 The `nameof` operator
### 3.1.12 Partial Types and Methods
**Partial Types** (e.g. partial class, partial struct) allow definition to be split into multiple files.
* e.g. a partial class is autogenerated using template; that class is enhanced with hand-authored content in another file
Syntax Required:
* Each participant (i.e. partial types) require `partial` declaration
* All participants cannot have conflicting member (i.e. same paramter, same return)
* Partial types are resolved at compile time.
e.g. partial type in different files
```csharp
// auto-generated file - PaymentFormGen.cs
partial class PaymentForm {...}
// hand-authored - PaymentForm.cs
partial class PaymentForm {...}
```
#### Partial methods
A partial type can contain **partial methods**. So autogenerated partial type can provide customizable hooks for manual change
Syntax of partial methods:
* consists of 2 part: a *definition* (in auto-generated file) and an *implementation* (in hand-authored file)
* If implementation is not provided, definition of the partial method is compiled anyway.
* Partial methods is `void`, and implicitly `private`, cannot include `out` parameter
```csharp
// In auto-generated file e.g. PartialFileGen.cs
partial class PaymentForm
{
...
partial void ValidatePayment(decimal amount); // Definition
}
// In hand-authored file
partial class PaymentForm
{
...
partial void ValidatePayment(decimal amount) // implementation
{
if (amount > 100)
...
}
}
```
#### Extended partial methods (C# 9)
TODO: Omit
### 3.1.13 The `nameof` operator
* `nameof` operator returns name of any symbol (type, memeber, variable, etc.) as a string
* Advantage of `nameof`: static type checking, e.g. if rename the symbol, all references will be renamed
```csharp
int count = 123;
string name = nameof(count); // name is "count"
```
## 3.2 Inheritance