C# Basics - Textnotes

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:

  1. using System; → Includes the System namespace (provides Console class).
  2. namespace MyApp → Groups related classes.
  3. class Program → Defines a class.
  4. static void Main(string[] args) → Main method, program entry point.
  5. 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
int4 bytesint age = 25;Integer numbers
float4 bytesfloat price = 10.5f;Single-precision decimal
double8 bytesdouble salary = 45000.75;Double-precision decimal
char2 byteschar grade = 'A';Single character
string-string name = "John";Sequence of characters
bool1 bytebool 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

  1. Constant: Value cannot change after compilation.

const double Pi = 3.14159;
Console.WriteLine(Pi);
  1. 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#

  1. Single-line comment: // This is a comment
  2. Multi-line comment:

/*
This is a
multi-line comment
*/
  1. 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:

  1. A C# program consists of namespaces, classes, and the Main method.
  2. Variables store data and have specific data types.
  3. Constants and readonly fields help protect values.
  4. Keywords are reserved words with predefined meaning.
  5. Comments improve code readability.
  6. Operators perform arithmetic, assignment, comparison, logical, and bitwise operations.