|
| 1 | +namespace CSharp14; |
| 2 | + |
| 3 | +// https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-14#the-field-keyword |
| 4 | +public class FieldBackedPropertySample |
| 5 | +{ |
| 6 | + public int FieldBackedProperty |
| 7 | + { |
| 8 | + get; |
| 9 | + set => field = value; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +public class SampleClass |
| 14 | +{ |
| 15 | + public required string Value { get; init; } |
| 16 | +} |
| 17 | + |
| 18 | +public static class SampleClassExtensions |
| 19 | +{ |
| 20 | + // Extension block |
| 21 | + extension(SampleClass sample) |
| 22 | + { |
| 23 | + // Extension property: |
| 24 | + public string ExtensionProperty => sample.Value; |
| 25 | + |
| 26 | + // Extension method: |
| 27 | + public void ExtensionMethod() |
| 28 | + { |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Extension block for static type |
| 33 | + extension(SampleClass) |
| 34 | + { |
| 35 | + // static extension method: |
| 36 | + public static void StaticExtensionMethod() |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + // static extension property: |
| 41 | + public static int StaticExtensionProperty => default; |
| 42 | + |
| 43 | + // static user defined operator: |
| 44 | + public static SampleClass operator +(SampleClass value1, SampleClass value2) |
| 45 | + => new() { Value = value1.Value + value2.Value }; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +public class UserDefinedCompoundAssignmentOperatorsSample |
| 50 | +{ |
| 51 | + public int? Value { get; set; } |
| 52 | + |
| 53 | + public override string? ToString() => Value?.ToString(); |
| 54 | + |
| 55 | + public void operator ++() { Value += 1; } |
| 56 | + |
| 57 | + public void operator --() { Value -= 1; } |
| 58 | + |
| 59 | + public void operator +=(UserDefinedCompoundAssignmentOperatorsSample c) { Value += c?.Value; } |
| 60 | + |
| 61 | + public void operator -=(UserDefinedCompoundAssignmentOperatorsSample c) { Value -= c?.Value; } |
| 62 | + |
| 63 | + public void operator *=(UserDefinedCompoundAssignmentOperatorsSample c) { Value *= c?.Value; } |
| 64 | + |
| 65 | + public void operator /=(UserDefinedCompoundAssignmentOperatorsSample c) { Value /= c?.Value; } |
| 66 | + |
| 67 | + public void operator %=(UserDefinedCompoundAssignmentOperatorsSample c) { Value %= c?.Value; } |
| 68 | + |
| 69 | + public void operator &=(UserDefinedCompoundAssignmentOperatorsSample c) { Value &= c?.Value; } |
| 70 | + |
| 71 | + public void operator |=(UserDefinedCompoundAssignmentOperatorsSample c) { Value |= c?.Value; } |
| 72 | + |
| 73 | + public void operator ^=(UserDefinedCompoundAssignmentOperatorsSample c) { Value ^= c?.Value; } |
| 74 | + |
| 75 | + public void operator <<=(int shift) { Value <<= shift; } |
| 76 | + |
| 77 | + public void operator >>=(int shift) { Value >>= shift; } |
| 78 | +} |
0 commit comments