Extension Methods in C#

| Dua Ali

Extension methods are, as the name implies, additional methods. Extension methods allow you to inject additional methods into a class or interface without having to alter, derivate, or recompile it. Extension methods can be added to your own custom class, .NET framework classes, or third party classes or interfaces.

For better understanding, here is an example project showing that how we can implement extension methods in C#.

Step 1: Open Visual Studio 2019, and create a new project by selecting Console Application.

Extension Methods in C#

Step 2: Provide a name to the project and click Next.

Extension Methods in C#

Step 3: Add an empty class to your project and name it. Here I have named it ‘TimeZoneAbbreviation’. Note that it is a static class as we are going to make extension method which itself is a static method. There is also a this keyword succeeded by a string data type that syntactically shows that it is an extension method.

Extension Methods in C#

Step 4: We can now call our extension method in our Main method.

Extension Methods in C#

Step 5: The console shows the final result. The string ‘CST’ is converted into its full form after it is passed to the extension method. In this way, we can use extensions with different implementations.

Extension Methods in C#

Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.


M
Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.


More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting