Skip to content

Commit c63a928

Browse files
committed
Small updates for .NET 10 and extension members
1 parent 0f57c84 commit c63a928

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

docs/pages/intermediate/extension-methods.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,74 @@ var ada = new Person("Ada", "Lovelace");
8484
ada.Print(); // "Ada Lovelace"
8585
```
8686

87-
However, with C#, we are limited only to methods and not properties; to achieve that, we can use [Partial Members](../bonus/partial-classes.md) instead.
87+
## Extension Members
88+
89+
C# 14 and .NET 10 introduce [extension members](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-14) as well.
90+
91+
This mechanism is extremely useful when working with third party types or even types from your own libraries where you may only want to extend the behavior without modifying the original.
92+
93+
<CodeSplitter>
94+
<template #left>
95+
96+
```ts
97+
// Class definition
98+
class Person {
99+
constructor(
100+
public readonly firstName: string,
101+
public readonly lastName: string
102+
) {}
103+
}
104+
105+
// Without this, TS will complain about the `displayName`
106+
interface Person {
107+
displayName: string
108+
}
109+
110+
// Extend with additional properties
111+
Object.defineProperty(Person.prototype, "displayName", {
112+
get() {
113+
return `${this.firstName} ${this.lastName}`;
114+
},
115+
});
116+
117+
const person = new Person("Ada", "Lovelace");
118+
console.log(person.displayName) // "Ada Lovelace"
119+
```
120+
121+
</template>
122+
<template #right>
123+
124+
```csharp{2,11-14}
125+
var contact = new Contact("Didi", "Cheng");
126+
Console.WriteLine(contact.DisplayName);
127+
128+
public record Contact(
129+
string FirstName,
130+
string LastName
131+
);
132+
133+
// 👇 Define one or more extension members here
134+
public static class ContactExtensions {
135+
extension(IContact contact) {
136+
public string DisplayName =>
137+
$"{contact.FirstName} {contact.LastName}";
138+
}
139+
}
140+
```
141+
142+
</template>
143+
</CodeSplitter>
144+
145+
One additional note is that in some cases, you may want to only add the extension members in the context of a single file. This can be useful when you need to add some utility members, but you do not want to pollute the general namespace.
146+
147+
```cs
148+
// 👇 Note the use of `file` here
149+
file static class ContactExtensions {
150+
extension(IContact contact) {
151+
public string DisplayName =>
152+
$"{contact.FirstName} {contact.LastName}";
153+
}
154+
}
155+
```
156+
157+
Not we can add this member only in the scope of the file.

docs/pages/intro-and-motivation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Many folks haven't followed the evolution of C# over the years after encountering it in the past when it was bound to the Windows-only ***.NET Framework*** (dark days...) and have strong, lingering misperceptions about C# and .NET today.
44

5-
Over the years, .NET and C# have evolved heavily (as has Microsoft) and now ***"modern .NET"*** -- e.g. ***.NET 9*** -- is open source, builds and deploys cross-platform with ease, and the development experience is well-supported whether you're on Windows, Mac, or Linux.
5+
Over the years, .NET and C# have evolved heavily (as has Microsoft) and now ***"modern .NET"*** -- e.g. ***.NET 10*** -- is open source, builds and deploys cross-platform with ease, and the development experience is well-supported whether you're on Windows, Mac, or Linux.
66

77
During that time, the language design of both C# and TypeScript (JavaScript as well) have *converged* more than other languages. The two languages are now remarkably similar in their core syntax such that developers that know one can typically pick up the other fairly easily.
88

@@ -235,7 +235,7 @@ In reality, C# is possibly the most natural choice for teams that are already ad
235235
One of the reasons .NET and C# get the side eye is that many developers, engineering managers, and CTOs may have come across C# and .***NET Framework*** at some point in their career, but haven't looked at it since Microsoft pivoted to the open source ***.NET Core*** initiative (that yielded the open source, numbered .NET versions). Today's .NET is very different from *.NET Framework* of the 2000's. Let's start with a simple distinction:
236236

237237
- `.NET Framework` - Win32 bindings, legacy, and primarily maintained and supported for legacy enterprise use cases. You'll see this as `.NET Framework 4.8.x`
238-
- `.NET 5`, `.NET 6`, `.NET 7`, `.NET 8`, `.NET 9`, etc - Modern, cross-platform .NET that runs on Linux, macOS, and Windows with x64 or Arm64 targets.
238+
- `.NET 6`, `.NET 7`, `.NET 8`, `.NET 9`, `.NET 10`, etc - Modern, cross-platform .NET that runs on Linux, macOS, and Windows with x64 or Arm64 targets.
239239
- `.NET Core`, `.NET Standard` - Designator of the bifurcation point between legacy and modern (long legacy of terrible nomenclature from Microsoft...)
240240

241241
::: info What's in a name?

src/csharp/csharp-notebook.dib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!meta
22

3-
{"kernelInfo":{"defaultKernelName":null,"items":[{"name":"csharp","languageName":"C#","aliases":["c#","cs"]},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"html","languageName":"HTML"},{"name":"sql","languageName":"SQL"},{"name":"kql","languageName":"KQL"},{"name":"mermaid","languageName":"Mermaid"},{"name":"http","languageName":"HTTP"},{"name":"value"}]}}
3+
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp","languageName":"C#","aliases":["c#","cs"]},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"html","languageName":"HTML"},{"name":"http","languageName":"HTTP"},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"kql","languageName":"KQL"},{"name":"mermaid","languageName":"Mermaid"},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"sql","languageName":"SQL"},{"name":"value"}]}}
44

55
#!csharp
66

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var contact = new Contact("Didi", "Cheng");
2+
Console.WriteLine(contact.DisplayName);
3+
4+
file static class ContactExtensions
5+
{
6+
extension(Contact contact)
7+
{
8+
public string DisplayName =>
9+
$"{contact.FirstName} {contact.LastName}";
10+
}
11+
}
12+
13+
public record Contact(string FirstName, string LastName);

0 commit comments

Comments
 (0)