C# is a simple and powerful programming language which is used to develop many robust applications. It is pronounced as C Sharp.
C# is supported by a large number of frameworks. C# can be used to develop a lot of web-based applications with the .NET Framework which is a software development platform developed by Microsoft. There are other frameworks like Mono or Xamarin which can be used to develop mobile apps, games or iOS and Android applications.
There are many features of C# which have made it one of the most widely used programming languages. It is an object-oriented language which is compatible with other .NET languages. It is type safe because a C# code can access only that part of memory which it has permission to access and execute, thus increasing the safety of the program.
Applications of C#
C# is used to develop a wide range of applications. Following are some of the applications developed using C# :
- Windows Client Applications → It is used to build Windows client applications using Windows Forms and WPF, which are application templates provided by Microsoft Visual Studio.
- Web Applications → It is used to build modern web applications using ASP.NET combined with JavaScript and other libraries and APIs. ASP.NET is one of the most widely used technology for building web applications and is supported by templates developed by Microsoft Visual Studio.
- Console Applications → It is used to build applications that run in a command-line interface.
- Azure Cloud Applications → It is used to build cloud-based applications for Windows Azure, which is an operating system of Microsoft for cloud computing and hosting.
- iOS and Android Mobile Apps → It is used for cross-platform native mobile app development via Xamarin, which can be used to create platform-specific UI code layer.
- Game Development → It is a quite popular language to build games because it is fast and has much control over memory management. It has a rich library for designing graphics. C# is used in many game engines like Unity Engine and Unreal.
- Internet of Things Devices → It is used for building IoT devices because it doesn't need much processing power.
- Artificial Intelligence → C# has extensive libraries for basic deep learning and embedded systems. As a result, it is used in the fields of Computer Vision and Artificial Intelligence.
How to start C#?
In this entire C# tutorial, you will be learning about writing C# programs. But what after that?
After writing any program, we need to compile and run it to see the output of the program.
So, what is meant by compiling?
When we write our program in C# language, it needs to be converted to machine language (which is binary language consisting of 0s and 1s) so that computer can understand and execute it. This conversion is known as compiling a program. We compile a code with the help of a compiler.
Integrated Development Environment (IDE)
To write and compile our C# program, we have been provided with many IDEs.
An IDE consists of a Text Editor as well as a Compiler. We type our program in the text editor which is then compiled by the compiler.
Text Editor
We write our program in a text editor. The file containing our program is called source file and is saved with a .cs extension.
C# Compiler
After saving our program in a file with the .cs extension, we need to compile it to convert it into machine language that computer can understand.
Now let's see how to compile and execute a C# program on different operating systems. We are only going to show how to run a C# program, the working of the code is explained in the next chapter.
Run C# on Windows
There are many IDEs available for editing and compiling C# programs in Windows like Microsoft Visual Studio and NetBeans. We will see how to run a C# code using Visual Studio and Command Prompt.
Visual Studio
We can edit and compile programs using Visual Studio which allows us to write, compile and run our program. Let's see how to do it.
Before learning to write a program in Visual Studio, first, make sure it is installed in your computer. If not, then download and install it from here.
1. On opening Visual Studio, you will get a window. Click on File → New → Project.
2. The New Project dialog box will appear. Expand Installed, then expand Visual C#, then select Windows Desktop, and then select Console Application.
3. Give the project name in the Name text box, and then click on OK button.
3. A file named Program.cs will get opened in the Code Editor. Replace its content with your C# program given below.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
4. To run the project, click on Debug → Start Without Debugging or press Ctrl+F5
keys.
5. A Command Prompt window will appear with "Hello World" written.
If you want to use the debugger, then put the breakpoint on the last line as shown below. Then to run the project, click on the Start button or press the F5 key.
Command Prompt
1. Write your C# program (code given below) in a text editor like Notepad and save it with a filename having .cs
extension. In our case, we named it as hello.cs.
using System;
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
2. Open the Command Prompt window (search for cmd).
3. To check if the path of the C# compiler is set as Environment Variable, type csc
and press Enter. You should get something as shown below.
If you are getting this message - 'csc' is not recognized as an internal or external command, operable program or batch file, then you need to the path of the C# compiler as Environment Variable.
4. To set the path as Environment Variable, right click on My Computer and select Properties.
5. Go to the Advanced System Settings tab.
6. Click on the Environment Variables button.
7. In the System Variables box, select Path and click on the Edit button.
8. An Edit Environment variable window will appear. Let's assume that csc.exe is located in C:\Windows\Microsoft.NET\Framework64\v3.5 directory. This directory is the path to the compiler.
Click on the New button and add the compiler path in the text box created as shown below. Then click on Ok.
9. Now open a new Command Prompt window. Use the command cd followed by a directory name to change your working directory. For example, if you are operating in C:\Users\John\Project and want to get to C:\Users\John\Project\prog ,then you need to type cd prog and press Enter. (This new directory should contain your previously created C# file (hello.cs in our case)).
10. Once you are in the correct directory in which you have your program, then you can compile and run your program.
To compile, type csc filename.cs
and press Enter. Here, our filename is hello.cs, so we will write csc hello.cs
. If it shows any error, then try to remove that error from your program and then again save and compile your program.
11. Once the program is compiled, run the program by typing filename
(hello
in our case) and then pressing Enter. We will get the output as shown below.
Run C# on Linux
For Linux, you can write your C# program in various text editors like Vim (or vi), Sublime, Atom, etc. To compile and run our C# program in Linux, we will use Mono which is an open-source implementation of the .NET framework.
So let's see how to create and run a C# program on Linux.
1. Open Terminal ( ctrl+alt+T
).
2. Type the command sudo apt install mono-complete
to install mono-complete.
3. Open a text editor (we are going to use Gedit) and save the following program with a .cs extension. We are going to name our file hello.cs, so we will open the file using gedit hello.cs
and write the following program and save it.
using System;
class Hello
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
4. Now, you can compile the program using mcs filename.cs
, so in our case, mcs hello.cs
.
5. To execute the program, write mono filename.exe
, so in our case, mono hello.exe
.
You can see "Hello World" printed on the screen.
Run C# on Mac
In Mac, you can use any text editor of your choice, we are going to use Atom. To compile and run our C# program in Mac, we will use Mono which is an open-source implementation of the .NET framework.
So let's see how to create and run a C# program on Mac.
1. Download and install Mono.
2. Open Terminal
3. Open a text editor (we are going to use Atom) and save the following program with a .cs extension. We are going to name our file hello.cs, so we will open the file using atom hello.cs
and write the following program and save it.
using System;
class Hello
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
4. Now, you can compile the program using mcs filename.cs
, so in our case, mcs hello.cs
.
5. To execute the program, write mono filename.exe
, so in our case, mono hello.exe
.
You can see "Hello World" printed on the screen.