C# Course

C# Variables and Data Types

A self-study guide for Python 3 compiled from the materials on this site. Primarily intended for those who want to learn the Python programming language from scratch.

Declaring Variables

int age = 25;
double price = 9.99;
bool isActive = true;
char grade = 'A';
string name = "Alice";

Built-In Types

C# Type.NET TypeSizeExample
intInt324 bytes42
longInt648 bytes9999999999L
floatSingle4 bytes3.14f
doubleDouble8 bytes3.14159
decimalDecimal16 bytes9.99m (money)
boolBoolean1 bytetrue / false
charChar2 bytes'X'
stringStringdynamic"hello"

var — Type Inference

var count = 10;         // int
var price = 9.99;       // double
var name = "Alice";     // string

// Useful in loops:
var numbers = new List<int> { 1, 2, 3 };
foreach (var n in numbers) Console.WriteLine(n);

Constants and readonly

const double PI = 3.14159265;   // compile-time constant
readonly int maxSize;            // set once in constructor

// PI = 3.0; // ❌ error

Nullable Types

int? nullableInt = null;
string? nullableStr = null;

// Null-coalescing operator
int value = nullableInt ?? 0;          // 0 if null

// Null-conditional operator
int? len = nullableStr?.Length;        // null if str is null

String Interpolation

string firstName = "Alice";
int age = 30;
string msg = $"My name is {firstName} and I am {age} years old.";
Console.WriteLine(msg);