Products Shop Support Company |

Mixing Mercury and C# (2:12)

Download the Video | View on YouTube

This is a video about Elements.
Watch more Elements Videos. Read more about Elements.


One of the cool things about Mercury — or Elements in general — is that it lets you mix code in different languages in the same project!

Say you've got this Visual Basic project, and you need to use a complex math algorithm you found online, but it's written in C#. No problem!

Just add a new source file to the project. Select the "Class" template, and change the language from Mercury to C#. Click "OK" and name the new file "MyCSharpClass".

Next, let's add a method that does our really complicated math.

Yeah, I know... but let's pretend.

\Going back to the main Program file, we can now use this class from Mercury, even though it's written in C sharp. Neat.

Lets build and run to see it work.

[he cool thing is that because these classes live in the same project, this can go both ways. Switching back to the C# class, we can directly access the Mercury code as well...

Of course this would never work with Visual C# and Visual Basic.NET, because the classes would have to be in two separate projects.

Finally, let's look at one more cool thing. What if you're a purist, and don't really like having C# code in your nice and clean Mercury project?

No worries. Just Right-click on the C# file and choose "Convert to Mercury"

And with that, sanity is restored!

Of course mixing languages works for all six Elements languages. Oxygene, C#, Swift, Java, Go, and Mercury.