string s = "Hello Extension Methods"; int i = MyExtensions. WordCount(s); The preceding C# code: Declares and assigns a new string named s with a value of "Hello Extension Methods" .
You can't do this with static classes, as you can't pass a static class as a parameter to a method. This isn't relevant to the question asked.
To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.
The problem here is not that you can't add an extension method to an abstract class (you can - you can add an extension method to any type) - it's that you can't add a static method to a type with extension methods. Extension methods are static methods that present themselves in C# as instance methods.
In C#, extension methods can be created by
public static class MyExtensions { public static ReturnType MyExt(this ExtType ext) { ... } }
Since all of my library are written in C++/CLI, I would like to create the .net extension methods also in C++/CLI (in order to have one DLL instead of two). I've tried the following code
static public ref class MyExtensions { public: static ReturnType^ MyExt(this ExtType ^ext) { ... } };
But the compiler can not recognize keyword 'this' in the first argument.
error C2059: syntax error: 'this'
Is there some way to create the extension method in C++/CLI ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With