June 21, 2023

C# - just try it

Back in April, I downloaded and installed "visual studio code" on my Fedora system. I can launch it by typing "code" at the command line.

In passing, I will note that there is "mono", which is an open source clone of .NET. It used to include an IDE "monodevelop", which was abandoned, but now taken up as "dotdevelop". But I don't know anything about all of this, nor have I fiddled with it.

This little adventure is me seeing how far I can get with it without watching endless videos and such. I type "code" and indeed it launches. It tells me that it sees that I have Docker installed and offers to set up Docker plugins. I have no idea why I should want that, so I am going to try to ignore or bypass it for now. Actually I change my mind and for no particular reason tell it to go ahead and install "Dev Containers". It seems to take only seconds.

The left edge of "code" offers various things, including "extensions", which is what activated to try to lure me into this docker business. The topmost of these is "explorer" and I switch to that.

Just for the record, here is a simple "hello world" in C#. Looks a lot like Java.

// Hello World! program
namespace HelloWorld
{
    class Hello {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}
I use File->New to create a new file, put it into /home/tom/C# and call it "hello". I copy and paste the above content into it. Apparently "cs" is the expected extension for C# files, so I should have called this "hello.cs". I exit from code, and rename the file using the linux "mv" command. I restart code and it now asks me if I want to install the recommended extension for C#. I click the "Install" button for this. I see a progress bar and it puts me again into the "extensions" thing. It is now telling me I need the .NET Core SDK, which makes sense. I click on "Get the SDK".

I takes me to a page on my browser:

It is offering me .NET 7.0 SDK and the browser has a button "Install for Linux". This takes me to a page with lots of options, including "packages for Fedora", when I select that, it takes me to this page: If you want to develop apps, you want the SDK. If you just want to run apps, the "runtime" will do. There is a command line tool "dotnet" that will tell you what you have installed via: "dotnet --list-sdks". If I already had some .NET installed this would work, but the command is not found (as one would expect).
Much to my surprise, this is a Fedora package, and I do:
su
dnf install dotnet-sdk-7.0
Last metadata expiration check: 2:54:58 ago on Wed 21 Jun 2023 11:20:19 AM MST.
Dependencies resolved.
================================================================================
 Package                           Arch      Version           Repository  Size
================================================================================
Installing:
 dotnet-sdk-7.0                    x86_64    7.0.105-1.fc38    updates     85 M
Installing dependencies:
 aspnetcore-runtime-7.0            x86_64    7.0.5-1.fc38      updates    7.3 M
 aspnetcore-targeting-pack-7.0     x86_64    7.0.5-1.fc38      updates    1.6 M
 dotnet-apphost-pack-7.0           x86_64    7.0.5-1.fc38      updates    3.9 M
 dotnet-host                       x86_64    7.0.5-1.fc38      updates    197 k
 dotnet-hostfxr-7.0                x86_64    7.0.5-1.fc38      updates    166 k
 dotnet-runtime-7.0                x86_64    7.0.5-1.fc38      updates     24 M
 dotnet-targeting-pack-7.0         x86_64    7.0.5-1.fc38      updates    2.7 M
 dotnet-templates-7.0              x86_64    7.0.105-1.fc38    updates    2.7 M
 netstandard-targeting-pack-2.1    x86_64    7.0.105-1.fc38    updates    1.3 M

Transaction Summary
================================================================================
Install  10 Packages

Total download size: 129 M
Installed size: 465 M
After this (it all seems to work), I can use the "dotnet" command:
dotnet --list-sdks
7.0.105 [/usr/lib64/dotnet/sdk]
As a quick side note. The newly installed "dotnet" command is /usr/lib64/dotnet/dotnet. The /usr/lib64/dotnet directory was newly created -- a bit of an odd way to do things on linux, but harmless I guess. /usr/bin/dotnet is just a link to this.

And further satisfying my curiousity, "code" is a link as follows:

/usr/bin/code -> /usr/share/code/bin/code

I monkey around a bit with VS code, but don't see any way to tell it to compile (and run) my program.

A tutorial for Visual Studio Code

Near as I can tell, there is "visual studio" and "visual studio code". Reading a bit, I learn that "VS code" is simply an editor, whereas "VS" is an IDE. This is disappointing. I don't want another editor, and I certainly hope I don't need one. Based on what this suggests, I install the C# Dev kit, which does seem to be listed under the available extensions. An install button is offered, so away we go. It tells me I will get version 0.1.103 or something like that. It now wants me to connect with my Microsoft account. It also wants me to upgrade my C# base language support. Now it gives me a "Get started" page that wants me to connect my account.

Now I am somewhat confused -- I haven't paid microsoft for C# and don't intend to. It also isn't clear how to "connect my account" at least there doesn't seem to be any button to start the process.

Start over ?

I search on "compile and run c# on linux" as well as "linux c# compiler command line". This article looks interesting: Based on this article, I didn't do anything extra, but I try this:
dotnet new console -o hello
cd hello
dotnet run
And indeed it prints "Hello, World!"

The Program.cs it generated for me, contains a comment line and one line of code, as follows:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
This is interesting, and not at all what I expected. But it does achieve my goal of running a C# program on linux.

It is worth noting that there is something called "mono" which seems to be an open source implementation (clone?) of the .NET framework. That offers an entirely different path to all of this.

The "dotnet" command

You can type "man dotnet" or "dotnet --help" for information

You can type "dotnet new --help" to get help on that specific command. (Hence also "dotnet run --help" is interesting.

Typing "dotnet new list" gives a list of all the installed "templates", including the console template that I used above.

Try some other things

I create a directory /home/tom/C# and do this:
mkdir C#
cd C#
dotnet new console -o console2
Now I got into this project and replace the "one line" Program.cs with the typical hello world code, with a couple of extra lines added -- namely this:
namespace HelloWorld
{
    class Hello {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
            System.Console.WriteLine("Hello Amigos!!");
            System.Console.WriteLine("Goodbye, for now anyway.");
        }
    }
}
I type "dotnet run" and it works!

Well, I have achieved my goal -- starting with source and running a C# program on my linux machine. Lots of black box though, but it is a starting point.

Here are some other sources of information. Apparently "dotnet" expects you to be working from whatever it is they call a "project". You don't just create a directory and throw some ".cs" files into it and then run the compiler by hand (or using your own build system).


Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org