C# (pronounced “c-sharp”) is a great coding language that works across Mac and PC. Programmers use it to build a variety of software applications, especially in the Windows environment.
In this article we provide a C# primer for Mac users. We show how to set up the language on your Mac, how to start a project, the basics of C# syntax and why you should choose this language in the first place. Plus our pick of the best online resources where you can learn more about the subject.
If this sort of thing appeals, you’ll be pleased to hear that we’ve got lots of other articles about coding on Mac, including Python and SQL, and another that helps you choose the best Mac for app development. There’s no excuse for not diving in and learning new skills.
What is C#, and why should I learn it?
C# is a programming language developed by Microsoft in 2000. It can be used for a range of development needs including desktop, app, web, mobile and game development. Unity, one of the biggest video game engines in the world, uses C# integration as standard, and Xamarin also uses C# to target multiple platforms with a single codebase.
So why would you want to learn C# on a Mac? There are lots of reasons.
While C# is tied to the Windows environment, it’s cross-platform so that you can learn it just as well on a Mac as a PC, and it opens many doors in the industry. If you’re coming from a development background, C# bears some similarity to Java: if you know one, the other won’t be too difficult. If you’re a beginner then it’s a great choice as it will give you a foundation for learning other languages.
C# is also a good option for people looking to get into games development. You use Unity, a 2D and 3D game engine and framework with C# (or JavaScript). You can use Unity to develop games for all platforms, including iOS.
Finally, because C# was created by Microsoft, this language is not going away any time soon.
Setting up C# on a Mac
The first step is to download an IDE. Visual Studio Code is probably the best choice out there, offering all of the functionality you need and is very simple to use.
First, you need to go to the Visual Studio website. Select Visual Studio Code from the four options and click the ‘Download for Mac’ button. Your download should now begin in the form of a zip file.
Once Visual Studio Code is downloaded, unzip the file and drag Visual Studio Code into your Applications folder. You can then open it from the same folder.
You will then need to download the C# extension, by opening the Extensions view inside Visual Studio Code using the shortcut ⇧⌘X (Shift + Cmd + X) or selecting Code > Preferences > Extensions.
You’ll notice a search bar at the top of the extensions view: type in “C#”. The one you need is by Microsoft, and is displayed in the screenshot below.
Click Install. Once that’s completed, restart Visual Studio and you’re ready to begin.
Starting a C# project on a Mac
To create a new project, open Terminal, which you can find inside Applications > Utilities on your Mac.
If you wanted to create a console application, as an example, you would type in ”dotnet new console -o consoleproject”.
Open up Visual Studio Code, and open up the folder you just created inside Terminal.
Open up the Program.cs file from the Explorer on the right to start programming.
Basic C# syntax
C# is a relatively simple language when it comes to grasping the basics. It isn’t as simple as Python, but it’s not as complex as C++.
Still, in order to be able to start coding and moving forward with using C# we need to cover a few basics. We will look at the most simple of all programming tasks, which is displaying text on the screen, and taking user input.
When you open up the Program.cs file, you’ll see the following lines of code:
All the code you need to write is going to go inside the Main() method. The code in there at the moment will write “Hello World” to the console, but we are going to change this.
Remove the line that says Console.WriteLine(“Hello World!”); and add the following two lines.
string greeting = “Hello everyone”; Console.WriteLine(greeting);
You’re setting up a new string called greeting and adding some text to the variable called “Hello everyone”. You’re then printing the greeting to the console. When you run your program, you should see that “Hello everyone” has been printed.
Next, we will change the app to make the greeting more personal by asking for the user’s name and then displaying “Hello” followed by whatever the user’s name is. Clear out all the code you just wrote, so you can start fresh.
First, we will need to declare a new string variable called name.
string name;
Next, we need to ask the user for their name.
Console.WriteLine(“What is your name?”);
We then need to get the input from the user, and save it to the variable we just created called “name”. To do this we use something called “Console.ReadLine();” which will take input from the user.
name = Console.ReadLine();
Once we have done this it is a simple case of outputting the data we have collected on to the screen. For this we will type the following:
Console.WriteLine(“Hello there, ” + name);
Because you used Console.ReadLine() and you are using Visual Studio Code, you will need to change an option inside the launch.json file before you run the program. Open up launch.json, and change the console configuration from “internalConsole” to “integratedTerminal” so that the program can read input.
If you run your program and enter your name, you should see your greeting displayed.
Mathematical calculations
If you wanted to do a mathematical calculation in C#, you could do something similar to the following:
int num1 = 12; int num2 = 14; int num3;
num3 = num1 + num2;
Console.WriteLine(num3);
Here we are declaring 3 integers. 2 of them have values (num1 is 12 and num2 is 14). The third integer called num3 doesn’t have a value when it’s first declared.
We then use num3 to store the value of num1 + num2, which will be 26. Finally, we print the value to the console. You could change the + to another operator such as * for multiplication or / for division if you fancy mixing it up.
If/Else statement
There will be times during development when you will want to check whether or not a certain condition is true. If you’ve developed in another language already, you will be familiar with this. Let’s have a look at the example below.
int a = 5; int b = 5;
if (a == b) { Console.WriteLine(“Yes the values are equal”); } else { Console.WriteLine(“Sadly, the values are not equal”); }
We start by creating two integers (a and b), both with the value of 5. We then create an if statement, so if a is equal to b then we print a message to the console saying “Yes, the values are equal”.
There is also an else statement, which would get printed if the values were not equal. This would say “Sadly, the values are not equal”.
However, in this instance, a and b both have the value of 5, so they are both equal. In this case, the first message would get printed.
You should now have a basic understanding of the C# syntax, and be able to start experimenting with your own programs. Why not try extending the code examples above into something new?
Other Mac programs you can use to build C# applications
• Fiddle: There’s an online compiler called .NET Fiddle that allows you to write and compile C# code. You should find a code window already open for you when you arrive.
• Rider: Jet Brains offer a fantastic cross-platform tool for .NET development. This will allow you to build out your C# applications in no time. You can get started by clicking here and hitting the Download button to get a 30-day trial.
• Unity: If you’re looking to build games in C#, you should try Unity. You can create all types of games, from 2D platformers all the way to VR experiences. Get started for free by clicking here.
The best online C# courses
Now that you’ve got C# set up on your Mac, you’re ready to start learning the C# language in more depth. There are a bunch of great sites, and online courses, ready to help. Here are some of the best to get you started:
• SoloLearn: One of the most in-depth resources around, SoloLearn is available inside the browser or through iTunes. It’s an interactive course that allows users to work at their own speed through a range of quizzes and source material, and can be found here.
• C# Basics for Beginners : Another excellent choice is this C# course on Udemy. With 5 hours of video available, it offers plenty of resources to help you understand the topics and move forward with your programming skills.
• Unity: If you’re interested in video game development then try Unity’s excellent library of resources, which can be found right here. It includes a range of information that will not just improve your coding skills but also allow you to develop them in a specific and exciting niche. No matter what your goals are in coding, there are enough resources on offer here to help you achieve them.
• Microsoft C# Programming Guide : Of course, there is no better place to learn than from the masters themselves. Microsoft offers its own online programming guide where no prior programming knowledge is required. Click here to visit the site.