Basic Programming with C#
Originally, I wanted to write the basic programming tutorial using C# as the language by my own. But, then I realised that there are already good tutorials. Youy can follow the following references, even the microsoft’s course also provide free certification. Maybe I will also write my own tutorial in times.
References:
- Microsoft learn (free certificate): link or link.
- More complete resource from Microsoft: link or link.
- Programmiz: link. Programmiz provides great tutorials, although, too many ads. So, yeah I will probably provide one which does not show any ads.
So, why c#
?
From my personal experience, c#
is way safer programming language compare to c
and c++
because it has garbage collector that help us to handle the memory allocation. I think it is also keeping the programming logic paradigm compare to some modern languages like python
.
What can we use c#
for? *not limited to.
- Web Development (with ASP.NET Core)
- Game Development (with Unity)
- Desktop Applications (with .NET MAUI, WPF, Windows Forms)
- Cloud and AI Services (with Azure)
*It works best in Visual Studio Community or VS Code.
C# Example
Here are some examples of C# syntax:
1. Variables
Imagine you need to store pieces of information, like a number, a name, or whether something is true or false. That’s what variables are for! They’re named storage locations in your computer’s memory.
int age = 30; // Stores a whole number (integer)
string name = "Alice"; // Stores text (string)
bool isActive = true; // Stores true or false (boolean)
2. Data Types
Each variable has a data type that tells the computer what kind of information it will hold. int
, string
, and bool
in the example above are just a few common data types.
3. Operators
Operators let you perform actions on your data.
- Arithmetic:
+
(add),-
(subtract),*
(multiply),/
(divide) - Comparison:
==
(equals),>
(greater than),<
(less than) - Logical:
&&
(AND),||
(OR)
int sum = 5 + 3; // sum will be 8
bool isAdult = age > 18; // isAdult will be true if age is 30
4. Conditional Statements
Computers can make decisions based on conditions! if statements allow your program to execute different blocks of code depending on whether a condition is true or false. Here, Console.WriteLine()
is a built function from .NET framework to help us displaying texts in our console.
if (age >= 18)
{
Console.WriteLine("You are an adult!");
}
else
{
Console.WriteLine("You are a minor.");
}
5. Loops
Sometimes you need to repeat a set of instructions multiple times. Loops are perfect for this! for
and while
loops are very common.
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Hello, C#!"); // This will print 5 times
}
Check out another Post for more.
Categories: Programming
Tags: Programming, C#