Introduction to C# Programming & The .Net Framework
C#, pronounced as c-sharp or see-sharp, is a high-level, robust, and modern programming language. C# is also a simple object-oriented programming language that exists within the .NET family of programming languages. It was developed by Microsoft under the .NET initiative led by Anders Hejlsberg = author of Turbo Pascal and Delphi, both of which are famous programming languages.
Like most programming languages, C# came about as an improvement over other programming languages especially the ones that had come before it such as C++, C, and the Java programming language. Because of this, C# may have a syntax that seems familiar to that of C, C++, and the Java programming language with slight difference in certain cases.
C# allows developers to create secure and robust applications for desktop, mobile, server, embedded systems, and web. In this tutorial, we are going to cover the major concepts of the C# programming language in detail, and in a way that will help you understand C# in a unique and easy way so that as you continue your learning journey, you can understand deeper aspects of the language.
Let us start by looking at how to setup C# on your computer and start creating C# programs. Before we begin this environment installation, you need to meet a few requirements necessary for running the C# Integrated Development Environment (IDE). For compatibility issues and efficient execution of the programs, ensure that your computer meets the following requirements:
- A 1.8Ghz and higher Processor – Preferably a 64-bit processor
- Windows 7 Service Pack 1 and higher
- 4GB of RAM and above (6GB for better performance)
- Minimum Disk Space of at least 8GB to 210 GB depending on the features you want installed
- Microsoft .NET framework 4.5 and higher
- For the initial setup, you need a relatively good internet speed.
However, you do not have to meet all the above requirements if you prefer to use a simple text editor such as VScode, Sublime, Vim or Atom.
How to install the .NET Framework
The very first action we are going to conduct is that of installing .NET framework 4.5 and higher. In this book, we are going to use version 4.7.
To download .NET framework, follow this link:
https://www.microsoft.com/en-us/download/details.aspx?id=55167
To begin the download, select your preferred language and click download. Once the download has completed, start the executable and begin the installation.
NOTE: If you are running updated versions of Microsoft Windows 8.1 and above, you may have .NET already installed and you can go ahead and cancel the installation. If case it is not, complete the installation.
Installing the IDE (C#)
Here, we are going to cover the installation of the C# IDE that contains a text editor and C# compiler binaries. We are going to use the Microsoft Visual Studio (MVS) as the IDE.
You can use a variety of MVS-environment alternatives such as JetBrains, Rider, and Mono IDE but some of these may require a subscription, which is why this book does not cover these installations.
Microsoft Visual Studio runs on Windows and Mac only (as of 2019). If you are a Linux user, you can download the Mono IDE, which is a very great alternative.
To download the visual studio installer, go to the following addresses for Mac and Windows users respectively
https://visualstudio.microsoft.com/vs/mac/
https://visualstudio.microsoft.com/vs/
Download the free, Community edition. In case of added features or commercial usage, you can purchase the Enterprise or Professional editions.
Once you get the installer, launch it, and wait until the initial setup completes and the visual studio installer launches.
Once done, another window such as the one shown below shall open:
Select the community version and click install.
Another window will launch requesting you to select the features to include during the IDE installation process.
For this case, we require the .NET desktop environment. Select it and click install. If you would like to install additional features, select them but ensure you have the required space for the installation.
Once you have selected the features to install, click install for the installation to begin. Wait until the process completes and then launch it.
In the previous section, we covered the requirements necessary to run C# programs as well as how to install the Environment you need to have in place to write C# code. If you face any issues during the installation, check out Microsoft visual studio forums for guidance.
C# Basic Concepts
Hello World Program and Basic C# Program Structure
Before we discuss the foundational concepts of the C#
programming language, let us look at how to create the most basic C# program. By doing this beginner project first, it will help you understand the basic layout and structure of a C# program.
To create a hello world program in Microsoft visual studio —the easiest-to-use program possible, Select create New project –> Console app (.NET CORE) and give the project a name. Since it is a simple program, call it helloworld
.
Once you have created the application, it will contain a file called Program with a .cs extension. This indicates that it is a C# source code file. By default, it will contain some code that looks as follows.
A basic C# program has five core features. These features are:
- Namespace
- Class
- Class methods
- Main Method
- Expressions
To run the code in Microsoft Visual Studio, select the Green play button in the Menu bar. Once the above program completes compiling, the text “Hello World” will appear printed in a console window.
Let us look at the features of the above program.
Using System:
This is usually the first part of a basic C# program. We use the keyword ‘using
’ to include the System namespace in the included program. ForJava
Programmers, this may interpret to theimport
packages or#include <header_file>.
A larger program may contain more than one using statements.- The second line in the program contains the namespace keyword or declaration that contains collection of
classes
. In the above program, thenamespace
HelloWorld contains theclass
called Program – which by default is named after the name of the file containing the source code. - The next line of the code contains the
class
declaration. Theclass
contains the methods and the data definitions of the program. It also contains more than one method used to define the behavior of the specifiedclass
. The HelloWorld program above contains a single method calledMain
. - Next is the
Main
method declaration. The main method is the entry point of all programs written in C#. As stated earlier, we use methods to define what the method does thus acting as the verb of the class. In this case, we used the method to print the text Hello World in the console. If you are familiar with Java, the main method looks like this.
public static void main(String[] args) {
System.out.println(“Hello world”);
}
As you can see, C# borrows a lot from Java and C++.
- The last line of code is the
Console.WriteLine()
-This line of code is the action which is broadcasted by the method main thus printing the text inside theWriteLine ()
function.
NOTE: For the above Example, we used the JetBrains Rider and the code may thus not have another code available in Visual studio. You may encounter Console.ReadKey()
that causes the program to wait for a key press before closing thus preventing the program from executing and closing immediately.
C# Basic Structure and Keywords
Like most modern high-level programming language, C# has its constraints and rules that you must follow as you program. Most of these constraints are like those of its ancestors – Java and C++ but slightly different. The following are some key points to note about C# programs.
- A C# program must contain a
main
method. - Execution of a C# program starts at the
Main
method - Unlike
Java
, the class name can be different from the filename containing the source code without causing any errors. - C# is case sensitive and thus the latter ‘Class’ and ‘class’ are two different words.
- You must terminate all C# statements with a semicolon
Those are the most basic and important rules in C# programming. Like other programming languages, C# also has specific keywords. In this case, and is the case with most programming languages, keywords are words reserved and used by the C# compiler and thus, you cannot directly use them as identifiers.
In programming, identifiers are unique words used to identify variables, classes, methods, and other data structure —we shall talk more about these in later sections.
The C# compiler reserves the following keywords for its use —different IDEs and text editors usually show them in different colors. However, you can use some of the keywords by adding the @ symbol at the beginning of the word. We not recommend this practice.
NOTE: In C#, some keywords may have special meaning when it comes to the context of the written code. We call these contextual Keywords. Below is a table of the most common C# keywords.
void |
explicit |
break |
using |
decimal |
continue |
join |
---|---|---|---|---|---|---|
abstract |
extern |
else |
enum |
try |
add |
into |
foreach |
bool |
sealed |
case |
event |
unchecked |
Gloab |
interface |
class |
checked |
for |
out |
base |
group |
as |
ulong |
do |
if |
sbyte |
uint |
| | `char` | `this` | `internal` | `in` | `randonly` | `alias` | |
while |
public |
operator |
null |
float |
sizeof |
| | `throw` | `stackalloc` | `int` | `in` | `namespace` | `ascending` | |
lock |
private |
unsafe |
switch |
struct |
descending |
| | `goto` | `true` | `long` | `false` | `set` | `from` | |
protected |
const |
out |
ref |
select |
volatile |
| | `implicit` | `new` | `finally` | `static` | `remove` | `dynaic` | |
ushort |
object |
override |
typeof |
delegate |
get |
| | `short` | `fixed` | `is` | `Virtual` | `orderby` | `let` | |
double |
return |
string |
Params |
partial |
byte |
Comments in C
Like most programming languages, C# allows us to use comments. The compiler ignores Comments and uses them to document the code for later use. Comments are very important since you can use them to refer to and explain the code if other programmers are working on it too.
You can create Comments in two formats:
- Single line comments: We create single line comments using two forward slashes (//)at the end of the line
- Multiline comments: We use Multiline comments to extend the comment to more than one line continuously. We create them using a single backlash and an asterisk and close them using an asterisk and a forward backslash (/* …*/)
NOTE: It is always a good practice documenting your C# code by using comments.
Conclusion
We have covered the basics of C# programming. However, the concepts discussed above are just bare bones of what C# entails. Check more upcoming C# guides.