Hello World in C#

Hello World in C# Featured Image

For our fifth installment of Hello World in Every Language, we’re tackling Hello World in C#. At this point, we’ve covered many of the main industry languages such as Java, C, C++, and Python. Now, let’s take on Microsoft’s C#.

Table of Contents

C# Background

As always, I took to Wikipedia to learn more about C#.

Just like our other programming languages so far, C# is a general-purpose language. However, unlike our other languages, C# was designed for the Microsoft platform as a part of the .NET initiative in 2000. We’ll see something along the same lines when we get to languages like Objective-C and Swift.

Some notable features of C# include the virtual and var keywords as well as properties and namespaces. Because of these features, C# can seem like a nice cross between Java and C++. In fact, C# is often considered a clone of Java.

Personally, I like C# much more than Java. I won’t go into too much of a tangent, but C# has this beautiful feature that allows us to declare getters and setters in a single line. Just take a look:

public string Name { get; set; }

Now, we have a string which we can get and set directly and safely without having to implement our own methods. I love this syntax.

Also, I love the fact that C# can be used to develop games in Unity. Beyond Android apps, I haven’t seen many fun commercial uses for Java.

Hello World in C#

Now, we can implement Hello World in C# in a couple of ways. For simplicity, I’ll share the minimalist approach but be aware that there are more complete ways to do this:

class HelloWorld {
  static void Main() {
    System.Console.WriteLine("Hello, World!");
  }
}

If you read the Hello World in Java tutorial, then this probably looks very similar. In fact, C# shares a lot of the same look and feel as Java. With that being the case, I’ll only highlight the major pieces here.

Before we can print, we have to create a class. Inside our class, we declare the main method. And inside our main method, we run our print command. The syntax and core libraries are a little different, but it feels eerily similar to Java.

How to Run the Solution

Perhaps the easiest way to run this solution would be to open up an online C# compiler. Just copy the code from above and drop it into the editor before hitting run.

Alternatively, we can download Visual Studio or Mono to run C# locally. Of course, we’ll want a copy of the solution as well. Refer to the manual of the various tools to run C#.

As far as I know, there aren’t any easy ways to run C# code from the command line, but I imagine it can be done.

Sample Programs in Every Language

With these first five articles finished, I can honestly say that anything past this point is new to me. Up next, I want to tackle Ruby. I hear that Ruby is also a general-purpose language, but it has a very popular framework used for web development. I’ll be focusing on the general-purpose language which we can run locally on the command line.

Series Navigation← Hello World in C++Hello World in Ruby →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.