C# Basics
C# Basics
1. Structure of a C# Program
A basic C# program has the following structure:
using System; // Namespace inclusion
namespace MyApp // Program namespace
{
class Program // Class definition
{
static void Main(string[] args) // Entry point
{
Console.WriteLine("Hello, C# Basics!");
}
}
}
Explanation:
using System;→ Includes theSystemnamespace (provides Console class).namespace MyApp→ Groups related classes.class Program→ Defines a class.static void Main(string[] args)→ Main method, program entry point.Console.WriteLine()→ Prints output to the console.
2. Variables & Data Types
Variables store data in memory. Each variable has a data type that defines the kind of data it can hold.
Common Data Types:
| TypeSizeExampleDescription | |||
| int | 4 bytes | int age = 25; | Integer numbers |
| float | 4 bytes | float price = 10.5f; | Single-precision decimal |
| double | 8 bytes | double salary = 45000.75; | Double-precision decimal |
| char | 2 bytes | char grade = 'A'; | Single character |
| string | - | string name = "John"; | Sequence of characters |
| bool | 1 byte | bool isActive = true; | True/False value |
Example:
int age = 25;
float price = 10.5f;
double salary = 45000.75;
char grade = 'A';
string name = "John";
bool isActive = true;
Console.WriteLine($"Name: {name}, Age: {age}, Active: {isActive}");
3. Constants & Readonly
- Constant: Value cannot change after compilation.
const double Pi = 3.14159;
Console.WriteLine(Pi);
- Readonly: Value can be set at runtime but cannot be changed afterward.
readonly int MaxValue;
public Program()
{
MaxValue = 100;
}
4. Keywords in C#
Keywords are reserved words with special meaning in C#. Examples:
int, float, double, string, class, namespace, public, private, static, void, const, readonly, if, else, for, while, switch, break, continue
5. Comments in C#
- Single-line comment:
// This is a comment - Multi-line comment:
/*
This is a
multi-line comment
*/
- XML comments: Useful for documentation.
/// <summary>
/// This method prints a message
/// </summary>
void PrintMessage()
{
Console.WriteLine("Hello");
}
6. Operators in C#
6.1 Arithmetic Operators
int a = 10, b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 1 (remainder)
6.2 Assignment Operators
int x = 5;
x += 3; // x = 8
x -= 2; // x = 6
x *= 2; // x = 12
x /= 3; // x = 4
x %= 2; // x = 0
6.3 Comparison Operators
int p = 10, q = 20;
Console.WriteLine(p == q); // False
Console.WriteLine(p != q); // True
Console.WriteLine(p > q); // False
Console.WriteLine(p < q); // True
Console.WriteLine(p >= 10); // True
Console.WriteLine(q <= 20); // True
6.4 Logical Operators
bool isTrue = true, isFalse = false;
Console.WriteLine(isTrue && isFalse); // False
Console.WriteLine(isTrue || isFalse); // True
Console.WriteLine(!isTrue); // False
6.5 Bitwise Operators
int m = 5; // 0101 in binary
int n = 3; // 0011 in binary
Console.WriteLine(m & n); // 1 (0101 & 0011)
Console.WriteLine(m | n); // 7 (0101 | 0011)
Console.WriteLine(m ^ n); // 6 (0101 ^ 0011)
Console.WriteLine(m << 1); // 10 (0101 << 1)
Console.WriteLine(m >> 1); // 2 (0101 >> 1)
Summary of Chapter 2:
- A C# program consists of namespaces, classes, and the Main method.
- Variables store data and have specific data types.
- Constants and readonly fields help protect values.
- Keywords are reserved words with predefined meaning.
- Comments improve code readability.
- Operators perform arithmetic, assignment, comparison, logical, and bitwise operations.