Why Use C#?
Why Use C#?
It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.
C# has roots from the C family, and the language is close to other popular languages like.
The first version was released in year 2002. The latest version, C# 13, was released in November 2024.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
In C#, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
string - stores text, such as "Hello World". String values are surrounded by double quotes
bool - stores values with two states: true or false
Names can contain letters, digits and the underscore character (_)
Names must begin with a letter or underscore
Names should start with a lowercase letter, and cannot contain whitespace
Names are case-sensitive ("myVar" and "myvar" are different variables)
Reserved words (like C# keywords, such as int or double) cannot be used as names
if (condition)
{
// block of code to be executed if the condition is True
}
if (condition)
{
// block of code to be executed if the condition is True
}
else
{
// block of code to be executed if the condition is False
}
if (condition1)
{
// block of code to be executed if condition1 is True
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
Example
variable = (condition) ? expressionTrue : expressionFalse;
Use the switch statement to select one of many code blocks to be executed.
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}
Loops
Loops are handy because they save time, reduce errors, and they make code more readable.
C# While Loop
The while loop loops through a block of code as long as a specified condition is True:
while (condition)
{
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
There is also a foreach loop, which is used exclusively to loop through elements in an array (or other
data sets):
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump
out" of a switch statement.
The break statement can also be used to jump out of a loop.
C# Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
Example
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
Console.WriteLine(i);
}
Create an Array
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value.
string[] cars;
To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly
braces:
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Example
cars[0] = "Opel";
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
// Now outputs Opel instead of Volvo
Array Length
To find out how many elements an array has, use the Length property:
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length);
// Outputs 4
If you are familiar with C#, you might have seen arrays created with the new keyword, and perhaps you
have seen arrays with a specified size as well. In C#, there are different ways to create an array:
// Create an array of four elements, omitting the new keyword, and without specifying
the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
It is up to you which option you choose. In our tutorial, we will often use the last option, as it is faster and
easier to read.
However, you should note that if you declare an array and initialize it later, you have to use the new
keyword:
// Declare an array
string[] cars;