C# Basics
Welcome to the fundamental concepts of C#. This section will guide you through the essential building blocks of the C# programming language, enabling you to write simple yet powerful applications.
1. The Anatomy of a C# Program
A basic C# program consists of one or more files containing code that is organized into classes and methods. The entry point for most applications is the Main
method.
A Simple "Hello, World!" Program
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Explanation:
using System;
: This directive imports theSystem
namespace, which contains fundamental classes likeConsole
.public class HelloWorld
: This declares a class namedHelloWorld
. In C#, all code resides within classes.public static void Main(string[] args)
: This is the entry point of the program.public
means it can be accessed from anywhere,static
means it belongs to the class itself (not an instance),void
means it doesn't return any value, andstring[] args
represents any command-line arguments passed to the program.Console.WriteLine("Hello, World!");
: This line uses theWriteLine
method from theConsole
class to print the string "Hello, World!" to the console.
2. Comments
Comments are used to explain code and are ignored by the compiler. C# supports single-line and multi-line comments.
Using Comments
// This is a single-line comment
/*
This is a
multi-line comment
explaining the next line.
*/
int myVariable = 10; // Variable declaration
3. Variables and Data Types
Variables are containers for storing data values. C# is a strongly-typed language, meaning you must declare the type of a variable before using it. Common data types include:
int
: For whole numbers (e.g., 10, -5).double
: For floating-point numbers (e.g., 3.14, -0.5).bool
: For boolean values (true
orfalse
).string
: For sequences of characters (e.g., "C# programming").char
: For single characters (e.g., 'A', '?').
Variable Declaration and Initialization
int age = 30;
double price = 99.99;
bool isStudent = true;
string name = "Alice";
char initial = 'A';
Console.WriteLine($"Name: {name}, Age: {age}");
Tip: Type Inference with var
You can use the var
keyword to let the compiler infer the data type of a variable. This can make your code more concise.
var message = "This is a string."; // Compiler infers string
var count = 100; // Compiler infers int
4. Operators
Operators are special symbols that perform operations on variables and values. Key operators include:
- Arithmetic Operators:
+
,-
,*
,/
,%
(modulo) - Assignment Operators:
=
,+=
,-=
,*=
,/=
- Comparison Operators:
==
,!=
,>
,<
,>=
,<=
- Logical Operators:
&&
(AND),||
(OR),!
(NOT)
Using Operators
int a = 10;
int b = 5;
int sum = a + b; // 15
bool isEqual = (a == b); // false
bool isGreater = (a > b); // true
Console.WriteLine($"Sum: {sum}, Is Equal: {isEqual}");
5. Control Flow Statements
Control flow statements allow you to control the order in which code is executed. Important control flow statements include:
- Conditional Statements:
if
,else if
,else
,switch
- Looping Statements:
for
,while
,do-while
,foreach
if-else
Statement and for
Loop
int score = 75;
if (score >= 90) {
Console.WriteLine("Grade: A");
} else if (score >= 80) {
Console.WriteLine("Grade: B");
} else {
Console.WriteLine("Grade: C");
}
for (int i = 0; i < 5; i++) {
Console.WriteLine($"Iteration: {i}");
}
6. Methods
Methods are blocks of code that perform a specific task. They help in organizing code and promoting reusability.
Defining and Calling a Method
public class Calculator
{
public static void Greet(string personName)
{
Console.WriteLine($"Hello, {personName}!");
}
public static int Add(int num1, int num2)
{
return num1 + num2;
}
public static void Main(string[] args)
{
Greet("Bob"); // Calling the Greet method
int result = Add(10, 20); // Calling the Add method
Console.WriteLine($"The sum is: {result}");
}
}