This .NET 8.0 console application demonstrates how to create and use C# extension methods to add new functionality to existing types without modifying their source code or using inheritance. The project shows how to extend HttpContext from ASP.NET Core with a GetBaseUrl() extension method that constructs the base URL from a request's scheme, host, and path base.
The example illustrates both calling the extension method as an instance method using the natural dot-notation syntax and calling it as a static method directly, demonstrating that extension methods are syntactic sugar over static method calls. This is a focused, minimal example that clearly shows the core concept of extension methods in C#.
- .NET 8.0 SDK or later
cd ExtensionMethodExamples
dotnet runIf you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:
Subscribe to Dev Leader Weekly
- All Links
- Website - Dev Leader
- YouTube - Dev Leader
- YouTube - Dev Leader Path To Tech
- YouTube - Dev Leader Podcast
- YouTube - CodeCommute
- Newsletter - Dev Leader Weekly
- LinkedIn - Nick Cosentino
- GitHub - ncosentino
- Twitter/X - Dev Leader
- Threads - Dev Leader
- Bluesky - Dev Leader
- Mastodon - Dev Leader
- Facebook - Dev Leader
- TikTok - Dev Leader
- Twitch - Dev Leader
- Stack Overflow - Nick Cosentino
Powered by BrandGhost 👻