Write Once, Run Anywhere: This is not Java, this is C #

Source: Internet
Author: User
Tags linux mint sharpdevelop

Note that the purpose of this article is not to provoke a language dispute. Although there is a vindication for C #, it is mainly about mono and simple testing.

updated:25th August 2012

Updated "compile Once, Run Anywhere: The ultimate cross-platform goal" section.

? Conmajia 2012

Introduction

"Write once, run Anywhere" (once written, running everywhere, Wora), sometimes written "write once, run Everywhere" (wore), is sun Microsystem (acquired by Oracle in 2010) is a slogan for promoting the cross-platform nature of the Java language. Ideally--often impossible--compiling a program written in the Java language into standard bytecode (bytecode) can run on any device that supports a Java Virtual machine (JVM).

Many dabbler Java "Experts" often use this to run. NET users, said they were "kidnapped by Microsoft, only the JVM this industry standard can cross the platform."

What is the real situation? On the one hand, real Java developers are constantly complaining about "write once, Debug Anywhere" (once written, debugged everywhere), on the other hand, more and more people realize. NET is essentially cli/cts, the industry standard, and the CLR is also a virtual machine. So, it is always a "cross-platform" ability to highlight Java and belittle. NET, is already outdated and do not understand the performance.

I recently deleted windows and installed Linux Mint (an Ubuntu-based Linux distribution) because my computer was running slowly.

In the Linux environment, there are very famous. NET run-time--mono.

The name of Mono, and the friends who make. NET know that. It makes. NET program has the possibility of running across platforms under Linux. Mono currently supports. NET v4.0, which has gradually stabilized and became popular (see Compatibility section). Since I only have C # (ashamed), I need to develop and run the. NET program under Linux and then install Mono.

$ sudo apt-get install Mono-gmcs libmono-system-data2.0-cil libmono-system-ldap2.0-cil Libmono-system-messaging2.0-cil Libmono-system-runtime2.0-cil

Here's an off-topic. Although for technologies that are already widely used, such as. NET), the size of the file at run time is not much of a discussion, but someone still has to say this to illustrate how the. NET Framework is bad (in fact, after win Vista this is no longer a thing). So what about Mono's performance?
Mono has a full installation size of 78MB (Java Minimum installation size 95MB), while mono minimizes installation requires 7MB. (Ref: Http://www.infoq.com/cn/news/2007/07/Mono-Runtime-Size)

In order to facilitate development, I installed the monodevelop directly. This is the Linux version of the famous open source. NET IDE sharpdevelop on Windows.

The installation commands are as follows:

$ sudo apt-get install MonoDevelop
Linux under compilation

Here are a few simple program tests. Note that the program code here is fully operational under Windows.

Command-line Programs

1 using System; 2   3 Namespace Test 4 {5     class program 6     {7         static void Main () 8         {9             Console.WriteLine ("Hello Mon O! "); Ten             console.readline ();         }12     }13}

Run results

WinForm Program

1 using System; 2 using System.Windows.Forms; 3  4 Namespace test 5 {6 Public     class Mainform:form 7     {         8         TextBox textBox1; 9         Button button1;10
   public mainform ()         {             textbox1=new TextBox ();             textbox1.text= "Text             here ..." Textbox1.location=new System.Drawing.Point (10,10),             button1=new Button (),             button1. Text= "click me.";             button1. Autosize=true;18             button1. Location=new System.Drawing.Point (10,40);             Controls.Add (textBox1);             Controls.Add (button1);     }23}

Run results

Isn't that a surprise? Under Linux, you can run the WinForm program directly. It's so convenient. The demo code is compiled under Linux and does not prove "Write once, run anywhere", so what if I run the exe compiled by Windows directly? Let's try out the ultimate goal of the cross-platform of compiled programs: Compile Once,run Anywhere

Compile Once, Run Anywhere: The ultimate cross-platform goal

Here's what I did before windows with Visual Studio and SharpDevelop compiled EXE without any processing (nor processing) to run directly.

First of all, "Hive wars", let's look at the effects of running under Windows.

Then it runs under Linux.

Note: Because the default. exe is associated with the archive manager, you need to select Open with "Mono Runtime".

Run the following effect

Tested for various functions normally. Indicates that GDI + works correctly, and that controls such as ToolStrip are functioning normally.

Take a look at another program I recently published: "InvokeHelper".

This is what happens under Windows.

This is the effect of running in the mono environment

The description and thread-related features work fine.

Again, it's related to Windows API. In fact, it is impossible to use the toes (not only C #, any language is the same, the Peace Platform API strongly related, how can "cross-platform" it).

"Get System Icon", this program uses shgetfileinfo this Windows API:

1 [DllImport ("Shell32.dll")]2 static extern int Shgetfileinfo (3   string pszpath,4   uint dwfileattributes,5   Ref   Shfileinfo psfi,6   uint cbfileinfo,7   uint UFLAGS8);

Working properly in Windows

What about under Linux? Run the next try:

Calling the Open File dialog box is OK, but will automatically exit once the Windows API is run. Therefore, after the platform, the Peace Station (WIN) related API is not used, it is also a matter of course. C # and Java have no way to jump off such a fate (laughter).

Compatibility

Here's an example of some of the current compatibility scenarios for Mono: Supported Paradigms (2.0+) and Var (3.0+).

The official compatibility can be viewed on this page: http://www.mono-project.com/Compatibility

Currently the newest mono is 2.10.8. (Released December 19th, 2011) can already support the. NET 4.0 version. See:

Transplantation

Choose a different platform, sooner or later to face the transplant problem. Since cli/cts only specifies the underlying part of the language, there are some differences in the implementation of each runtime (see previous section: Compatibility). So, Mono officially provides a transplant helper called mono migration Analyzer (MOMA, MO). This tool can directly tell you the possibility of porting a ready-made Windows + Microsoft.NET-based program to Win/linux/mac + mono.

Sometimes the implementation of a small function, there are many ways to implement, but some of the implementation is dependent on the Windows API, and some not, without compromising performance, we prefer to choose the standard implementation rather than the special implementation. This is the recipe for success using mono as a project.

Summarize

At present, the more famous non-Windows platform under the. NET Virtual machine/runtime only mono, portable.net (thanks @ Crane Soaring), I believe that over time, there will be more runtime appear, mono will become more powerful. When it comes, it's more than just java,c# and. NET platform of various languages (VB, C + +/CLI, F #, etc.) can be implemented "Write once, run anywhere". Of course, there's the "Debug Anywhere" (laughter) that comes with it.

Write Once, Run Anywhere: This is not Java, this is C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.