This topic is give you a very example source code about class extension!
You have one library write from another developer, and you want to add new method into this dll class, so how do we do it?
Explore My Other Channel for More Cool and Valuable Insights
π Youtube Learn Tech Tipsπ Tiktok
π Facebook:Ex: I have two solution
One is CalculatorLibrary (dll class) and another is ClassExtensionExample (Main winform application)
- The CalculatorLibrary dll have one class Calculator with two methods: Plus and Sub
Calculator.cs (CalculatorLibrary solution)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculatorLibrary
{
public class Calculator
{
public int Plus(int x, int y)
{
return x + y;
}
public int Sub(int x, int y)
{
return x - y;
}
}
}
On main application side, we'll create a new multiply method extension to CalculatorLibrary:
Form1.cs (ClassExtensionsExample solution)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassExtensionExample
{
public static class CalculatorExtensions
{
public static long Multiply(this CalculatorLibrary.Calculator calc,
int x, int y)
{
return x * y;
}
}
}
NOTE: you must use static class and use this pointer here to description about this extension class is extension for Calculator DLL
After you define it, when you use it, you'll see same image:
You will see the Multiply extension method appear.
You can download full source code here for details view
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
A fun and easy guide to making your own extension methods in C# ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips, all things Tech Tips for web development
- I am Zidane, See you next time soon ✋✋✋✋