Welcome back! Now that you’ve set up your development environment, it’s time to dive into the very basics of C# syntax. Understanding these fundamentals will provide a solid foundation for your coding journey. Let’s break down the key components of C# programming with simple, easy-to-follow examples.
1. Structure of a C# Program
A C# program is typically structured with namespaces, classes, methods, and statements. Here’s a simple example:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Breakdown:
- using System;: This line includes the System namespace, which contains fundamental classes and base classes.
- namespace HelloWorld: Namespaces are used to organize code. Here,
HelloWorld
is the namespace. - class Program: Classes are the building blocks of C#.
Program
is the class containing our code. - static void Main(string[] args): This is the entry point of the program where execution starts. It’s a method named
Main
that takes an array of strings as an argument. - Console.WriteLine(“Hello, World!”);: This line outputs “Hello, World!” to the console.
2. Variables and Data Types
Variables are used to store data. C# is a strongly-typed language, meaning you need to specify the type of data a variable will hold.
Example:
using System;
class VariablesDemo
{
static void Main(string[] args)
{
int number = 10; // Integer
double pi = 3.14; // Double
char letter = 'A'; // Character
string greeting = "Hello"; // String
bool isCSharpFun = true; // Boolean
Console.WriteLine(number);
Console.WriteLine(pi);
Console.WriteLine(letter);
Console.WriteLine(greeting);
Console.WriteLine(isCSharpFun);
}
}
Data Types:
- int: Stores integers.
- double: Stores floating-point numbers.
- char: Stores a single character.
- string: Stores a sequence of characters.
- bool: Stores a boolean value (
true
orfalse
).
3. Operators
C# supports various operators for performing operations on variables.
Arithmetic Operators:
int a = 5;
int b = 3;
Console.WriteLine(a + b); // Addition
Console.WriteLine(a - b); // Subtraction
Console.WriteLine(a * b); // Multiplication
Console.WriteLine(a / b); // Division
Console.WriteLine(a % b); // Modulus
Comparison Operators:
int x = 10;
int y = 5;
Console.WriteLine(x > y); // Greater than
Console.WriteLine(x < y); // Less than
Console.WriteLine(x == y); // Equal to
Console.WriteLine(x != y); // Not equal to
Console.WriteLine(x >= y); // Greater than or equal to
Console.WriteLine(x <= y); // Less than or equal to
Logical Operators:
bool isTrue = true;
bool isFalse = false;
Console.WriteLine(isTrue && isFalse); // Logical AND
Console.WriteLine(isTrue || isFalse); // Logical OR
Console.WriteLine(!isTrue); // Logical NOT
4. Control Structures
Control structures allow you to control the flow of your program.
Conditional Statements:
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not an adult.");
}
Switch Statement:
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Other day");
break;
}
Loops:
For Loop:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
While Loop:
int j = 0;
while (j < 5)
{
Console.WriteLine(j);
j++;
}
Do-While Loop:
int k = 0;
do
{
Console.WriteLine(k);
k++;
} while (k < 5);
5. Arrays
Arrays are used to store multiple values in a single variable.
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Example with Foreach:
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Conclusion
Congratulations! You’ve just learned the very basics of C# syntax. This foundational knowledge will help you as you move forward into more advanced topics. Practice these concepts by writing simple programs, and you’ll be well on your way to becoming proficient in C#.
Stay tuned for more tutorials where we’ll dive deeper into C# and explore more complex concepts and projects. Happy coding!