• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C++ Equivalent to C# Extension Methods

C++ doesn't have extension methods, so a C# extension method is just converted to an ordinary C++ static method (calls to the method have to be adjusted to static calls using the class name).

C# C++
public static class ContainsExtension
{
    public static void ExtensionMethod(this string myParam)
    {
        //...
    }
}
class TestClass
{
    void TestMethod()
    {
        string s;
        s.ExtensionMethod();
    }
}
#include <string>

class ContainsExtension final
{
public:
    static void ExtensionMethod(const std::string &myParam)
    {
        //...
    }
};
class TestClass
{
private:
    void TestMethod()
    {
        std::string s;
        ContainsExtension::ExtensionMethod(s);
    }
};


Use C# to C++ Converter to convert from C# to C++.     

Additional resource: C++ and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.