-
Notifications
You must be signed in to change notification settings - Fork 6k
Create tutorial for C# 14 extension members #48896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Tagging @adegeo because he gave me some ideas for extensions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR creates a comprehensive tutorial for C# 14 extension members, a new language feature that enhances the existing extension methods functionality by enabling properties, operators, and type-level extensions.
Key Changes:
- Added a new tutorial demonstrating extension members syntax and capabilities
- Created supporting code samples comparing traditional extension methods with new extension member features
- Updated the table of contents to include the new tutorial
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
File | Description |
---|---|
docs/csharp/whats-new/tutorials/extension-members.md | Main tutorial document explaining extension members concepts and syntax |
docs/csharp/toc.yml | Added navigation entry for the new extension members tutorial |
docs/csharp/whats-new/tutorials/snippets/PointExtensions/*.cs | Code samples demonstrating both traditional extension methods and new extension members |
@@ -0,0 +1,190 @@ | |||
--- | |||
title: Explore extension members in C# 14 to enhance existing types | |||
description: "C# 14 enables provides new syntax for extensions that support properties and operators, and enables extensions on a type as well as an instance. Learn to use them, and how to migrate existing extension methods to extension members" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected grammatical error 'enables provides' to 'enables' or 'provides'.
description: "C# 14 enables provides new syntax for extensions that support properties and operators, and enables extensions on a type as well as an instance. Learn to use them, and how to migrate existing extension methods to extension members" | |
description: "C# 14 provides new syntax for extensions that support properties and operators, and enables extensions on a type as well as an instance. Learn to use them, and how to migrate existing extension methods to extension members" |
Copilot uses AI. Check for mistakes.
ms.topic: tutorial | ||
ms.date: 10/06/2025 | ||
ai-usage: ai-assisted | ||
#customer intent: As a C# developer, I reduce repeated code by introducting extension members for common tasks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'introducting' to 'introducing'.
#customer intent: As a C# developer, I reduce repeated code by introducting extension members for common tasks | |
#customer intent: As a C# developer, I reduce repeated code by introducing extension members for common tasks |
Copilot uses AI. Check for mistakes.
cd PointExtensions | ||
``` | ||
|
||
1. Copy the following code into a new filed named `ExtensionMethods.cs`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'filed' to 'file'.
1. Copy the following code into a new filed named `ExtensionMethods.cs`: | |
1. Copy the following code into a new file named `ExtensionMethods.cs`: |
Copilot uses AI. Check for mistakes.
public void Rotate(int angleInDegress) | ||
{ | ||
double theta = ((double)angleInDegress * Math.PI) / 180.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'angleInDegress' to 'angleInDegrees'.
public void Rotate(int angleInDegress) | |
{ | |
double theta = ((double)angleInDegress * Math.PI) / 180.0; | |
public void Rotate(int angleInDegrees) | |
{ | |
double theta = ((double)angleInDegrees * Math.PI) / 180.0; |
Copilot uses AI. Check for mistakes.
public static void Rotate(this Point point, int angleInDegress) | ||
{ | ||
double theta = ((double)angleInDegress * Math.PI) / 180.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'angleInDegress' to 'angleInDegrees'.
public static void Rotate(this Point point, int angleInDegress) | |
{ | |
double theta = ((double)angleInDegress * Math.PI) / 180.0; | |
public static void Rotate(this Point point, int angleInDegrees) | |
{ | |
double theta = ((double)angleInDegrees * Math.PI) / 180.0; |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tutorial @BillWagner! I left primarily style changes for you to consider.
|
||
The extension operators make point arithmetic as natural as working with built-in numeric types. | ||
|
||
## Add More operators |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Add More operators | |
## Add more operators |
|
||
## Migrate instance methods to extension members | ||
|
||
Extension members also support instance methods. You don't have to change existing extension methods. The old and new forms are binary and source compatible. If you want to keep all your extensions in one container, you can. Migrate traditional extension methods to the new syntax maintains the same functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extension members also support instance methods. You don't have to change existing extension methods. The old and new forms are binary and source compatible. If you want to keep all your extensions in one container, you can. Migrate traditional extension methods to the new syntax maintains the same functionality. | |
Extension members also support instance methods. You don't have to change existing extension methods. The old and new forms are binary and source compatible. If you want to keep all your extensions in one container, you can. Migrating traditional extension methods to the new syntax maintains the same functionality. |
|
||
## Related content | ||
|
||
* [Extension methods (C# Programming Guide)](/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* [Extension methods (C# Programming Guide)](/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) | |
- [Extension methods (C# Programming Guide)](/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) |
## Related content | ||
|
||
* [Extension methods (C# Programming Guide)](/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) | ||
* [What's new in C# 14](/dotnet/csharp/whats-new/csharp-14) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* [What's new in C# 14](/dotnet/csharp/whats-new/csharp-14) | |
- [What's new in C# 14](/dotnet/csharp/whats-new/csharp-14) |
|
||
* [Extension methods (C# Programming Guide)](/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) | ||
* [What's new in C# 14](/dotnet/csharp/whats-new/csharp-14) | ||
* [Operator overloading (C# reference)](/dotnet/csharp/language-reference/operators/operator-overloading) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* [Operator overloading (C# reference)](/dotnet/csharp/language-reference/operators/operator-overloading) | |
- [Operator overloading (C# reference)](/dotnet/csharp/language-reference/operators/operator-overloading) |
Point rotatePoint2 = new Point(5, 5); | ||
Console.WriteLine($"Before Rotate: {rotatePoint2}"); | ||
rotatePoint2.Rotate(45); | ||
Console.WriteLine($"After Rotate(45�): {rotatePoint2}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLine($"After Rotate(45�): {rotatePoint2}"); | |
Console.WriteLine($"After Rotate(45°): {rotatePoint2}"); |
Point rotatePoint3 = new Point(3, 4); | ||
Console.WriteLine($"Before Rotate: {rotatePoint3}"); | ||
rotatePoint3.Rotate(180); | ||
Console.WriteLine($"After Rotate(180�): {rotatePoint3}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLine($"After Rotate(180�): {rotatePoint3}"); | |
Console.WriteLine($"After Rotate(180°): {rotatePoint3}"); |
Point rotatePoint1 = new Point(10, 0); | ||
Console.WriteLine($"Before Rotate: {rotatePoint1}"); | ||
rotatePoint1.Rotate(90); | ||
Console.WriteLine($"After Rotate(90�): {rotatePoint1}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLine($"After Rotate(90�): {rotatePoint1}"); | |
Console.WriteLine($"After Rotate(90°): {rotatePoint1}"); |
Point rotatePoint2 = new Point(5, 5); | ||
Console.WriteLine($"Before Rotate: {rotatePoint2}"); | ||
rotatePoint2.Rotate(45); | ||
Console.WriteLine($"After Rotate(45�): {rotatePoint2}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLine($"After Rotate(45�): {rotatePoint2}"); | |
Console.WriteLine($"After Rotate(45°): {rotatePoint2}"); |
Point rotatePoint3 = new Point(3, 4); | ||
Console.WriteLine($"Before Rotate: {rotatePoint3}"); | ||
rotatePoint3.Rotate(180); | ||
Console.WriteLine($"After Rotate(180�): {rotatePoint3}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLine($"After Rotate(180�): {rotatePoint3}"); | |
Console.WriteLine($"After Rotate(180°): {rotatePoint3}"); |
Create a tutorial to explore C# 14 extension members.
Fixes #45612
Internal previews