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 WikipediaOpens in a new tab. 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# compilerOpens in a new tab.. Just copy the code from above and drop it into the editor before hitting run.

Alternatively, we can download Visual Studio or MonoOpens in a new tab. to run C# locally. Of course, we’ll want a copy of the solutionOpens in a new tab. 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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts