C# is one of the most widely used programming languages in the world, ranking consistently as the fifth most popular language on the Tiobe index. It is only beaten by such juggernauts as Python, Java, and JavaScript. In this tutorial, we are going to learn some of the basics of C#, including how to create our own (obligatory) Hello, World application.
This program will make the assumption that you have installed an integrated development environment (IDE) or code editor and that you are familiar with creating a simple C# file. If you do not have an IDE installed and configured, never fear: our article how to install and configure Microsoft Visual Code can walk you through the steps in under five minutes.
One other note: it does not matter which code editor you choose. Again, if you are having trouble choosing, you can read our picks at our article Best IDEs for .Net Developers.
Creating Your First C# Program
As with all programming languages, it is customary to create your first application and call it “Hello, World!” as a way of sort of introducing yourself to the world. It is akin to learning how to play “Smoke on the Water” on guitar – it is a rite of passage. However, we are going to think bigger and introduce ourselves to the universe at large instead. Dream big!
The Hello, World program serves a single purpose: print some text to the user’s screen. It does not get simpler than that. In C#, printing text to a user’s screen is accomplished using what is known as a type to represent the console window. This console type, further, has a method called writeline that actually prints the text we tell it to print to the console.
Here is how to write your first C# program. Type the following code into your .Net IDE and the Run it once finished:
Console.WriteLine("Hello, Universe!");
When you run that C# example code, the following output should appear in the console:
Hello, Universe!
Congrats, you have created your first ever computer program using the console type and the writeline method!
Read: Commenting Best Practices in C#
Introduction to C# Variables
Writing a single line of code to print a single line of text to the console is great and all, but the real power of C# – and any programming language really – is the ability to store and retain information for later use and manipulation. One of the ways C# stores information is through an object known as a variable. The best way to think of a variable is to imagine a box. You can put something in that box, remove the thing from the box, and place it back inside the box. You can also replace the thing in the box with something else if you so choose. You can only, however, hold one thing at a time in this particular box.
Variables have types that determine what sort of information they will hold. One type of data is known as a string and its purpose is to hold text values, which can consist of any type of character you type into it. The caveat here is that if you enter a number or numeric digit into a string variable, it will be treated as a letter and not a number, so you will not be able to perform math on it or use it in the same way you would traditionally use numbers.
One last note: variables are called variables because their data can vary.
Read: C# Data Types Explained
Here is how we can use a few string variables to print multiple lines of text to the console using C#:
string firstName = "James"; string lastName = "Payne"; Console.WriteLine(firstName); Console.WriteLine(lastName);
Here, we have created two string variables – firstName and lastName – which carry a person’s first and last name, respectively. We then pass – or give – those variables to Console.WriteLine to print the value of the variables to the console. This will result in the following output when you run the program:
James Payne
At any time, we can choose to add a different value to our variables. Consider if I got a name change. Observe the following code:
string firstName = "James"; string lastName = "Payne"; Console.WriteLine(firstName); Console.WriteLine(lastName); firstName = "Maximus"); Console.WriteLine(firstName); Console.WriteLine(lastName);
A few things to note about this code. First, as before, we created two string variables to store our first and last name values. Then we printed them out to the console. Next, we changed the value of firstName to “Maximus” and reprinted the first and last name values. This would give us the following result:
James Payne Maximus Payne
Since we changed the value in firstName, going forward it will now read “Maximus” versus “James”. One more note about this sample C# sharp code: we did not define the firstName variable when we changed its value to “Maximus” – it is alread assigned – or initialized as a string type, so we no longer need to tell C# what kind of data that variable holds. Indeed, if we did try to create it as a string again, it would result in an error.
We can also mix our variables in with other text to append it to the end of a sentence, as an example. Consider this code:
string firstName = "Maximus"; string lastName = "Payne"; Console.WriteLine("Behold, I am " + firstName + " " + lastName + "!");
Here, we print some text using Console.WriteLine and then append our two variables using the + operator. Note that we also append a space by using ” ” and add an exclamation point (!) by adding “!” to the line as well. Here is the results when we run this sample code:
Behold, I am Maximus Payne!
Truly, a sight to behold.
Read: Working with Strings in C#
Other C# Programming Tips
That is all the time we have for this round. Check back often as we will be continuing our series on C# programming for beginners!