If you read the previous post (the Java Edition), you saw how simple it was to write a simple program. I would like to continue this trend and show you how to create the same program – one that simply prints “Hello, World!” to the screen and exits – in C#. Remember, this is a four-part series, and the four languages I’ll be covering are:

  • Java
  • C#
  • PowerShell
  • Visual Basic

So what makes C# so different from Java?

In its simplest form, C# is geared more towards the rapid development of full console-based applications and “Fat Apps” (or, apps with a GUI). While Java can do a lot of this, developers typically use it for behind-the-scenes components, such as web scripts and applets. Syntactically, the languages are very similar. For example, here’s the code for a “Hello, World!” program in C#:

When we run this program, the following is displayed:
helloworld-cs

The first difference you will probably notice from the Java HelloWorld App is the inclusion of a namespace block.

In C#, namespaces are regions of code, which organize identifiers such as classes and other members within. Namespaces are not required for a program to function, but just like indenting isn’t required, it’s a wise choice to make code as organized as possible… in case your codebase someday grows extra-large.

Now the actual code begins:

Just like in the Java example, the code begins with a class declaration. In this case, the class is called Program instead of HelloWorld, but the concepts are the same – this is the class where the code begins its execution.

This is alarmingly similar to the main void declaration shown in the Java example! The main difference is the lack of the word public. In C#, a top-level class can only be public or internal. Best practices, and conventional wisdom, state that a class should not be escalated to public unless there is an actual need. This is similar to how you don’t leave permissions wide open on your sensitive computer, router, or bank account. Declaring a class as internal prevents it from being accessed via other files outside of the same assembly. As with the Java example, the string[] args simply declares an array, of type String, that stores all command-line parameters passed to the program at its launch. I will be going over classes (in detail) with a later post.

This is the line of code that actually prints text to the screen. In C#, the Console object allows you to read from the standard input stream (usually a keyboard). It allows you to write to the standard output and standard error streams, which are usually displayed on the screen or written to a file.

This simply causes the program to wait for the user to input a line of text. I’m not doing anything with the resulting input, so this code essentially makes the program pause until the user hits Enter. (A similar line of code was NOT needed for the Java project, because my particular tool of choice, TextPad, always automatically ends my Java program with a “Press any key to continue…” prompt.)

That’s all there is to writing your first C# program! It’s really simple, and hopefully not as scary as it may have seemed.

Share

Comments are closed.

Post Navigation