Can a C# program be cross-platform? - c#

I'm a newbie to programming, and I'm considering using C# to write a VERY simple program that simply edits a text file.
Will it work on Macs and Linux? What will work?
I want to know what makes a program cross-platform?

C# can be compiled for and ran on multiple platforms (Windows, Linux, macOS) by using .NET Core.
Previously, cross-platform development using C# could only be done by using Mono which is a third-party implementation however it is now recommended to use .NET Core as it is developed by Microsoft.

Now C# is fully cross platform and has variety of frameworks and run times.
Java is a language + framework. But C# is just a modern language. C# language features are very powerful, easy to learn. No confusion between framework and language.
C# can be used in following frameworks
.Net core (Multi platform supports Linux, Windows and Mac), mostly used to develop restful services, web applications and enterprise systems
.Net Framework (Windows only), Same as .net core, but has more windows only features
Unity - One of most used game engine to develop games for PC, Sony, Xbox devices etc..
Xamarin - To develop native Mobile apps (Android, IOS, Mac)
.Net standard - Create a library in .net standard use in all above applications
Java - Code once, Ship same file to all platforms
C++, C, Rust - Code once, Compile for specific platform
C# - It is just a language, pick your framework according to requirement.
Most popular applications like Chrome, Firefox uses C++, Rust and they are compiled for each platform. But still they share the majority of code between these platforms. Some games use C++ to share the code between IOS and Android.
For new developers its good to start with NodeJS and C#. Both are easy and can do any type of applications.

It's hard to answer something like that to somebody, which states that he just starts programming, but I'll try to give you an (somewhat simplified) overview: C# is a language, which is compiled into an executable program. The language itself is portable between plattforms, as long as you have a compiler on each target plattform. On Windows you have the "default" .Net implementation and on Linux/Mac you have Mono. To do something useful - like editing files - you have to use some libraries. There is a standard library which is available on each plattform. As long as you use only that functionality, your program will be cross plattform. Obviously is will not be cross plattform, as soon as you use some library specific to a certain plattform. Editing files is part of the standard library, so you can implement such simple tools in a cross plattform way. When choosing a book, you should look for books which focus on the "raw" language and not on Visual Studio or other specific tools.

A new open source release from Microsoft called CoreCLR is cross-platform and can now be used for this
The CoreCLR repo contains the complete runtime implementation for .NET Core. It includes RyuJIT, the .NET GC, native interop and many other components. It is cross-platform, with multiple OS and CPU ports in progress.
-from the CoreCLR repo

I would not recommend c# as the top choice for multiplatform coding. I would recommend python instead it is very easy to run even as a script and works perfectly on all platforms. If you insist on c# be prepared to deal with the same problem explained by Justin Ethier. Python on the other hand, is the same for all platforms, it is super powerful fast and even has wrappers ready to connect to other languages.

C# is cross-platform thanks to .NET Core, but without a cross-platform User Interface library you can only make very basic console programs with it. In order to make cross-platform apps for Windows, Mac and Linux you need a cross-platform User Interface library like Nevron Open Vision

Yes, C# is fully cross-platform thanks to .NET Core.
.NET Core is a free and open-source, managed computer software framework for Windows, Linux, and macOS operating systems. It is the cross-platform successor to the .NET Framework which only run on Windows. The project is primarily developed and supported by Microsoft (through the .NET Foundation).

C# is cross platform and you can run your program in Windows/Linux and Mac. For Linux, though, you need to run it using mono

Related

Invoke C# code from a Swift or ObjC application [duplicate]

Currently, we are developing a .NET Standard class library that will be used by different Xamarin.* platforms.
I was wondering if there is a way to use the shared code in a native Android project (maybe produce an AAR/JAR file).
The reason I need this functionality is because our company provides an AAR to one of our customers (that is used by a native Android project), and we would like to keep a single source base instead of two.
Thanks in advance!
Update:
The name changed to ".NET Embedding" and it is at v0.4 at the time of this update.
https://developer.xamarin.com/releases/dotnetembedding/dotnetembedding_0/dotnetembedding_0.4/
New instructions:
Installing .NET Embedding
Original post:
You can use Mono's Embeddinator-4000
It supports various language consumers, so it surfaces .NET code as idiomatic code in the target language. This is the list of supported languages at present:
Objective-C: mapping .NET to idiomatic Objective-C APIs.
Java: mapping .NET to idiomatic Java APIs.
C: mapping .NET to an object-oriented like C APIs.
Embeddinator-4000 is a tool that allows your existing .NET Code (C#, F# and others) to be consumed from other programming languages and in various different environments.
Java is currently in a preview state for macOS, Windows, and Android.
The platform is selected by passing the --platform= command-line argument to the embeddinator. Currently macOS, Windows, and Android are supported.
re: https://mono.github.io/Embeddinator-4000/
As a part of the requires For Android you will need Xamarin.Android 7.4.99+ and Android Studio 3+ (Java 1.8) but if you are building Android apps with Xamarin and Java you would have those already.
Note: This tool is in Preview, so review the open issues and post new if needed.
re: https://github.com/mono/Embeddinator-4000/issues
Note: This is not trans-compiling, but embedding Mono's runtime in a native package that allow CIL code, AOT'd (i.e. iOS) or not to run within a different language/runtime.

what are my options for multiplatform GUI applications in C#?

Keep in mind that although I know how to code, I am NOT a professional developer. moving on:
I am looking to create a simple GUI program for an embedded system that runs on both Linux and windows. The issue is, the person responsible for developing the applet insists on it being written in c#. I can run .net framework code through mono, and the .net core is multiplatform by design, but he was going to use UWP, and that obviously won't work on Linux. I have looked into Avalonia, but it is not compatible with the version of .net he uses. He told me that QT apparently won't work for some complex reason b/c we are just running a local app and nothing web-connected or even from a server. To be very clear, I need cross-platform GUI in c#. I am also not asking what the best option is, just what possible options even exist. I and the dev will go from there.
thx in advance, RUWO
You have some options which depending on your needs might work well, I would go with AvaloinaUI, but here are your options:
Avalonia Is a WPF-inspired cross-platform XAML-based UI framework providing a flexible styling system and supporting a wide range of OSs: Windows (.NET Framework, .NET Core), Linux (also ARM), MacOS. Android and iOS are on the Roadmap.
There is also a functional wrapper for Avalonia https://github.com/AvaloniaCommunity/Avalonia.FuncUI that makes using it from F# a charm.
Gtk#
is a Graphical User Interface Toolkit for mono and .Net. The project binds the gtk+ toolkit and assorted GNOME libraries, enabling fully native graphical Gnome application development using the Mono and .Net development frameworks.
Eto This framework can be used to build applications that run across multiple platforms using their native toolkit, with an easy to use API. This will make your applications look and work as a native application on all platforms, using a single UI codebase.
CoreUI/LibUI Bindings to portable C Library. Simple and portable (but not inflexible) GUI library in C that uses the native GUI technologies of each platform it supports.
QtSharp Mono/.NET bindings for Qt
This project aims to create Mono/.NET libraries that wrap Qt (https://qt-project.org/) thus enabling its usage through C#.
Avalonia is currently heading towards version 1.0 but it's already stable. I am using Avalonia now since about a year, and my experience is pretty good so far. The community around it is growing fast, and the core developers are very active in the official gitter chat.
.NET Maui is now an option as of recently, and is developed by Microsoft.

How to write programs in C# .NET, to run them on Linux/Wine/Mono?

In this particular case I need to run .complicated NET application for Linux. By complicated i mean - that project was developed for 3 years and i don't want to write it again in java or something else and develop and support both, .NET and Java version later.
Application is generating mouse and keyboard events (by winapi/dll import) and uses serial port. I have also few timers for delays - for serial port communication (10-20ms, i dont need big precision here).
The rest is just a lot of simple code, nothing special, no weird controls, no directx etc.
What should i expect? Will this work?
If some part of code will fail - i can change it a little, make network connection between .net app and mini-application on linux that i can write for sending mouse and keyboard events, or RS232 communication.
Additionally i want to ask about Wine and .NET in general:
How to write .NET applications that should run on Linux/Wine/Mono?
which version of framework? (1.x, 2.0 or can i use 3.5?)
what should i avoid (imports from windows dll? timers?)
Edit/moved from comment:
I saw mono few years ago, but it was terrible. Now i see it growed up, supports LINQ, Threading and other complicated features. In addition now help looks really serious. Im not accepting answer yet, because i see that people still posting very useful links. If this question gets many +1 i will rewrite it and maybe this will help others.
I hope someone here have some practical experience with .NET on linux here...
Have you read about Mono.NET on http://www.mono-project.com?
It allows you to write .NET apps for Linux with minor differences over the Microsoft implementation.
I don't think wine will cope with any of the .NET components.
Just read the documentation before and you have a go.
PS: This also gives you the compatibility list between mono and .NET. http://www.mono-project.com/Compatibility and MoMA(mono migration analyzer) is a tool that will scan any .NET app already created to see if its compatible to deploy on linux.
In this post i shortened and combined all information i learned from others in this thread.
Open source impementation of .NET for Linux is called Mono.
Compatibility is well documented (see links below). Mono supports LINQ, threading and some other complicated features.
It even supports InteropServices. Its possible to use DllImport on linux library (!), like libc.so for example.
Mono can support Android and iOS (they are Linux based, right?)
There is a tool for Mono compatibility analysis called MoMA (Mono Migration Analyzer)
The Mono Migration Analyzer (MoMA) tool helps you identify issues you
may have when porting your .Net application to Mono. It helps pinpoint
platform specific calls (P/Invoke) and areas that are not yet
supported by the Mono project.
Useful links:
Mono - home page
Mono - page with documentation
Mono - compatibility page
MoMA - Mono Migration Analysis page
We have a couple of .net applications that run on Windows or Linux with Mono.
You can develop in VisualStudio if you want, it is not necessary to use the Mono development environment. Only the runtime mono libraries are required in order to run .net applications on Linux.
We use net 2.0 target to compile these apps in VisualStudio, because mono is nearly 100% compatible with .net 2.0. Mono also implements APIS from later .net versions, but not all and not as complete. If you use features of newer versions than 2.0, you should check if these are supported or use the Mono compatibility analysis tool. You can also use Mono develop instead of Visual Studio, my predecessor was doing that; the result is pretty much the same, and I prefer to use Visual Studio instead.
We didn't find any major trouble, and the apps are relatively complex, heavily multithreaded, async I/O, WinForms, sockets, serial ports, etc.
Some app used .net libraries that are not available in MS .net, but all these can be installed on Windows as well so that there is no problem to run these apps in Windows.
Hope that helps,
You can develop C# applications on linux with .NET Core: https://www.microsoft.com/net/core
After you install it, type dotnet new in your terminal to get a list of application templates which can be generated for you to start.
As of .NET Core 2.0 today, this is the list:
Templates Short Name Language Tags
--------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
Unit Test Project mstest [C#], F#, VB Test/MSTest
xUnit Test Project xunit [C#], F#, VB Test/xUnit
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App razor [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
global.json file globaljson Config
Nuget Config nugetconfig Config
Web Config webconfig Config
Solution File sln Solution
Razor Page page Web/ASP.NET
MVC ViewImports viewimports Web/ASP.NET
MVC ViewStart viewstart Web/ASP.NET
The current latest version is 2.0.
You can install the full .Net Framework 4.x in wine using winetricks, then run your app.
Wine supports serial ports, so I don't see any inconvenience.
I made a video showing this:
https://www.youtube.com/watch?v=60yyWBLXv1k
In the video, you can see the windows taskbar. Don't worry about it. This is because it is running in WSL2. But these steps work perfectly in Linux.
Regards

CLR IN MOBILE ----is it possible to use virtual machine on mobile os

as jvm makes java as plateform independent is it possible with CLR to make .net framework supported languages like c# plateform independent on mobile
Yes, you can use the Mono CLR. Mono is the open source implementation of C#, .NET BCL and the CLR.
MonoTouch runs on the iPhone
MonoDroid runs on Android devices
Yes-ish. At the tooling level, see MonoDroid, MonoTouch or the WP7 tools. However, in each case you are using the language to target a specific mobile platform. You may need different UI code for each, even if the core code is shared.
.NET Compact Framework works on mobile devices (and has for about 8 years now). .NET is also cross-platform (kind of) through the Mono and MonoTouch projects.
Technically, yes, but each handset has it's own limitations that may not make it very feasible.
If you look at the mono-project, it's a portable implementation of the CLR which can target mobile architectures, but it's still an area of development and not very mature yet.
MonoDroid is the project targetting Android handsets, but it's in a beta stage at the moment.
MonoTouch can target iOS, but it cannot take full advantage of the framework. Apple's licensing prevents virtual machines from running on the iOS, so .NET applications need to be AOT (Ahead-of-time) compiled to run on it. This works, but you won't be able to use parts of the BCL like the reflection API.
There's some early development for mono to support WebOS (Palm) too.

Is C# platform neutral?

Today I purchased C# 3.0 Pocket Reference (O'Reilly Publishers).
In that book in the first para of the first page it is given that "The C# language is
platform neutral, but it was written to work well with Microsoft .Net platform"
If I am not wrong, Platform Neutral mean that the softwares made from that language should run in all the OS(e.g.Mac, Windows,Linux etc.).
I know that this characteristic is being satisfied by Java but .Net has not yet been able to do that(Though MONO has made some progress in Linux).
Then what is the significance of that word "PLATFORM NEUTRAL" so far c# is concern?
Thanks in advance
The C#-language itself is platform neutral like C and C++ are - you can implement a compiler et al for it on any platform.
The .Net platform and its associated libraries however, which it was designed to work well with, are not platform neutral in design - they are designed to work on windows, although Mono does compensate somewhat for that.
If you would write a C# program without using any features of any .Net-library it is guaranteed to be platform-independent - you can be sure it will run on any platform that has tools for C#... you just won't have a particularly useful program though ;)
C#, the language, is platform neutral.
But the frameworks usually associated with C#, the dotNEt environment, are not. They run on Microsoft's OS-es.
Mono enables C# on many other (mostly unix-oid) platforms by providing the necessary runtime environment, but it doesn't come with (all) the frameworks needed to run typical C# programs.
Don't forget you can use C# in:
MS .NET (Windows)
.NET Compact Framework (PocketPC, XBox 360)
Silverlight (Windows, Mac)
Moonlight (Mono / Linux)
MonoTouch (iPhone)
MonoDevelop (various including android)
Micro Framework (some watches etc)
Not exactly bad coverage. The language has very few requirements on the runtime / OS.
C# is platform neutral in the sense of OS (i.e., OS neutral) if you count Mono as a reliable runtime on Linux.
But, C# and .Net can run as it is, without recompilation, on both 32 and 64 bit machine. I guess this is the meaning of the author.
In this case, platform neutral is defined as "being able to run on any machine that has a compatible virtual machine". Depending on what language features and compiler you use, C# could be considered able to run on machines running recent versions of Windows, Mac OS and Linux.
C# the language can be implemented on any platform which has a compatible compiler and virtual machine. The language itself has an ECMA standard for implementation, and thus does not itself contain any platform (i.e. Microsoft) specific language elements.
Before answering the question, one must consider that any language these days is useless without its libraries. That said many namespaces that make up part of are windows only. Many are just wrappers around microsoft technologies, like com, win forms, etc.
All things considered just take a look at the adjustments mono has made to provide alternatives to dot net. Miguel has mentioned many times that they hope Silverlight will win rather than other ui toolkits because it is truely platform independent rather than wpf and winforms etc.
If you want a true multi platform language and environment look at Java.
In my view, Microsoft's claim that C# is platform neutral (repeated by the text you are quoting from) is disingenuous.
It is technically correct, but in practice the limited portability of C# applications to non-MS operating systems has happened despite Microsoft rather than because of them. It is pretty clear that Microsoft has no intention of supporting C# / .Net on any non-MS operating system. Indeed, many people think that Microsoft (via its patent licensing deal with Novell) is using C# / Mono as a "wedge" to damage the GNU / Linux ecosystem.
IMO, anyone thinking of using Mono needs to consider: 1) the risks to their project if MS decides to play hardball about .NET patents, and 2) the damage they might be doing to the open-source ecosystem as a result.

Categories

Resources