Variables, Data Types & Operators
Master C# data types, variable declaration, and operators for effective programming. This is a foundational concept in Microsoft application framework that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world .NET experience. Take your time with each section and practice the examples
C# Data Types
C# is a strongly-typed language, meaning every variable must have a specific data type. Understanding data types is crucial for writing correct and efficient C# code.. This is an essential concept that every .NET developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Value Types
// Integer types
byte age = 25; // 0 to 255
short temperature = -10; // -32,768 to 32,767
int count = 1000; // -2,147,483,648 to 2,147,483,647
long population = 8000000000L; // Very large numbers
// Floating-point types
float price = 19.99f; // 7 digits precision
double pi = 3.14159265359; // 15-16 digits precision
decimal money = 123.45m; // 28-29 digits precision (financial)
// Character and boolean
char grade = 'A'; // Single character
bool isActive = true; // true or false
// DateTime
DateTime now = DateTime.Now;
DateTime birthday = new DateTime(1990, 5, 15);Reference Types
// String
string name = "John Doe";
string message = "Hello, World!";
string multiline = @"This is a
multiline string";
// Arrays
int[] numbers = { 1, 2, 3, 4, 5 };
string[] names = new string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
// Objects (we'll learn about classes later)
object anything = "This can be anything";
anything = 42;
anything = true;Variable Declaration and Initialization
// Explicit type declaration
int age = 25;
string name = "John";
bool isStudent = true;
// Implicit type declaration (var keyword)
var age = 25; // Compiler infers int
var name = "John"; // Compiler infers string
var isStudent = true; // Compiler infers bool
// Multiple variable declaration
int x = 10, y = 20, z = 30;
string firstName = "John", lastName = "Doe";
// Constants
const double PI = 3.14159;
const string COMPANY_NAME = "MyCompany";
// Nullable types
int? nullableInt = null;
string? nullableString = null;Operators in C#
// Arithmetic operators
int a = 10, b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
// Assignment operators
int x = 10;
x += 5; // x = x + 5 (15)
x -= 3; // x = x - 3 (12)
x *= 2; // x = x * 2 (24)
x /= 4; // x = x / 4 (6)
// Comparison operators
bool isEqual = (a == b); // false
bool isNotEqual = (a != b); // true
bool isGreater = (a > b); // true
bool isLess = (a < b); // false
bool isGreaterOrEqual = (a >= b); // true
bool isLessOrEqual = (a <= b); // false
// Logical operators
bool condition1 = true;
bool condition2 = false;
bool andResult = condition1 && condition2; // false
bool orResult = condition1 || condition2; // true
bool notResult = !condition1; // false