News

Projects Seek To Extend Reach of C#

Apple's young Swift programming language is getting a lot of attention and being put to new uses as it evolves, but Microsoft's .NET stalwart C# language isn't exactly sitting still.

For example, several projects seek to extend the reach of the language by converting C# code into other languages, such as C++ and JavaScript.

Here's a look at two of those projects recently in the news that provide such translation, albeit for quite different purposes: CoreRT and Bridge.NET.

CoreRT
This open source project to extend the reach of C# is part of a greater effort on the part of Microsoft to provide .NET tooling that runs on Mac and Linux OSes in addition to Windows.

Microsoft developer Jeffrey T. Fritz explained the project in a blog post last week, in which he explained the current two-step process for creating .NET apps, involving a compiler that emits intermediate code (IL) that is in turn just-in-time (JIT) compiled to produce native code running on any target OS that supports the common language runtime (CLR).

The idea is to simplify that process to more easily produce native Windows, Mac and Linux apps without relying on the CLR, through ahead-of-time (AOT) compilation and "CodeGen" instead of using JIT.

"If we want to shortcut this two-step compilation process and deliver a 100 percent native application on Windows, Mac and Linux, we need an alternative to the CLR," Fritz said. "The project that is aiming to deliver that solution with an ahead-of-time compilation process is called CoreRT."

CoreRT
[Click on image for larger view.] CoreRT (source: Microsoft)

That project is based on RyuJIT, but that technology is limited to applications in which .NET is available, and it needs to be tweaked to handle other OSes or environments.

"If I really want to write some C# code and have it 'just work' on a new IoT device, I don't have any options until the RyuJIT is capable of generating machine code that works with that processor and operating system," Fritz said.

"One strategy we are investigating is the ability to transpile .NET code to C++ code that could be compiled with an appropriate C++ compiler for the target platform," he continued. "This strategy opens doors for hardware development because you would no longer need to wait for a Microsoft engineer to extend our compilers to support some new OS or new hardware in order to allow .NET developers to work with your product."

Fritz noted the complexity involved with CodeGen and transpilation of C# code and gave an update on the team's progress so far. For example, the CoreRT team this summer made progress in transpiling .NET interface code and dispatching it to C++. An interface is a C# construct that somewhat resembles a class but is used to provide a contract to define signatures of properties, methods, events and indexers that must be supported in classes and structs that implement an interface to provide specific functionality.

Although transpiling C# interface usage to C++ might seem simple, Fritz said it involved overcoming several coding challenges.

Despite those challenges, the CoreRT project can now transpile interface usage in a baseline scenario, for which the code can be found on GitHub.

"We believe that investing in C++ code generation is going to allow the .NET ecosystem to grow and support platforms that have not been invented yet," Fritz said. "With this approach, we no longer need to write JIT compilers for every environment, and can rely on the standardized and robust C++ compilers that are commonly available. To learn more about CoreRT and the CodeGen processes being constructed, follow the CodeGen label on the CoreRT issues repository. "We'll have more about CoreRT in the future as this framework progresses."

Bridge.NET
Another project that's progressing is Bridge.NET, which provides an open source C#-to-JavaScript compiler and associated frameworks to enable developers to create apps that run on any device that supports JavaScript, whether thy run iOS, Android, Windows, Mac or Linux.

In that regard, it can be viewed as an alternative to Xamarin, whose technology was recently acquired by Microsoft to help Visual Studio developers create native iOS and Android mobile apps solely with C#. Bridge.NET is available on GitHub under an Apache 2.0 license.

Bridge.NET Transpilation of 'Hellow World' (Using Deck.NET Online Playground)
[Click on image for larger view.] Bridge.NET Transpilation of 'Hellow World' (Using Deck.NET Online Playground) (source: Bridge.NET)

Released in March of last year by Object.NET Inc., Bridge.NET was recently the focus of a blog post published on the Dice.com careers site, written by David Bolton.

He noted that while JavaScript is object-based, the language had no support for classes -- an object-oriented programming mainstay -- until the recent implementation of ECMAScript (ES6).

"Before ES6, JavaScript's big problem was difficulty in maintaining very large applications," Bolton said. "In order to get around this, many developers have coded in an object-oriented language that uses classes, and then ported the code to JavaScript. It's simply easier to maintain large applications with decent class hierarchies; in addition, many programmers also don't properly use prototypal inheritance [used in JavaScript]."

Along with Xamarin and several other projects, Bridge.NET lets developers use traditional object-oriented constructs in C# and target non.NET platforms with transpiled JavaScript code.

"Bridge.NET wasn't the first C#-to-JavaScript transpiler -- there are others -- but it has the advantage of having been professionally developed by full-time programmers," Bolton said. "It's a platform worth exploring in detail for anyone who works with both C# and JavaScript, and perhaps wants to make their programming endeavours a little more streamlined. Having been written in C#, Bridge.NET transpiles faster than most other transpilers (except those written in C/C++ such as Emscriptem)."

Available in downloads of about 5MB for Visual Studio and Visual Studio Code, it also can be installed via the NuGet package manager from those coding tools. The downloads page indicates the project hit version 15.3.0 on Monday, just days after the Microsoft blog post about CoreRT. It supports some of the more popular JavaScript frameworks, such as JQuery and Bootstrap, with further support on tap.

Developers wanting to explore the technology can use the online Deck.NET "C# to JavaScript Compiler Playground" in which they can type C# code and instantly see it transpiled into JavaScript.

"Bridge.NET is impressive," concluded Bolton. "It works as stated and appears very robust. For an open source project it's pretty slick. Also, the online browser version is fun because you get the generated JavaScript back almost instantly.

"The only things I feel are lacking are a lot more examples and more detailed documentation. Also, while this aimed at C# developers, prior knowledge of JavaScript is probably a necessity if you want things to move efficiently."

About the Author

David Ramel is an editor and writer for Converge360.