Master the fundamental concepts of C# programming with our comprehensive beginner's guide.
Learn how to store and manipulate data using variables and understand C#'s basic data types.
Learn NowMaster arithmetic, comparison, logical, and assignment operators to perform operations on your data.
Learn NowControl the execution of your program using conditional statements (if/else) and loops (for/while).
Learn NowOrganize your code into reusable blocks using methods with parameters and return values.
Learn NowVariables are containers for storing data values. In C#, you must declare the type of a variable when you declare it.
// Integer types int age = 25; // Whole numbers long bigNumber = 10000000000L; // Large whole numbers // Floating point types float price = 19.99f; // Single precision double pi = 3.1415926535; // Double precision // Boolean bool isCSharpFun = true; // true or false // Character char grade = 'A'; // Single character // String string name = "Code Crusader"; // Text // Var keyword (type inference) var message = "Hello World"; // Compiler determines type
Use meaningful variable names like userAge instead of ua to make your code more readable.
using System; class Program { static void Main(string[] args) { // Declare variables for a user profile string userName = "Crusader123"; int userAge = 28; double accountBalance = 1250.75; bool isPremiumMember = true; // Display the information Console.WriteLine($"Username: {userName}"); Console.WriteLine($"Age: {userAge}"); Console.WriteLine($"Account Balance: ${accountBalance}"); Console.WriteLine($"Premium Member: {isPremiumMember}"); } }
Create a program that stores information about a book (title, author, year published, and price) and displays it.
Operators perform operations on variables and values. C# has several types of operators for different purposes.
using System; class Program { static void Main(string[] args) { // Arithmetic operators int sum = 10 + 5; // +, -, *, /, % int power = (int)Math.Pow(2, 3); // 2^3 = 8 // Assignment operators int x = 10; x += 5; // x = x + 5 // Comparison operators bool isEqual = (10 == 5); // ==, !=, >, <, >=, <= // Logical operators bool result = (10 > 5) && (20 < 30); // && (AND), || (OR), ! (NOT) // Ternary operator string message = (x > 10) ? "Greater than 10" : "10 or less"; } }
using System; class Program { static void Main(string[] args) { Console.Write("Enter first number: "); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine($"Addition: {num1 + num2}"); Console.WriteLine($"Subtraction: {num1 - num2}"); Console.WriteLine($"Multiplication: {num1 * num2}"); Console.WriteLine($"Division: {num1 / num2}"); Console.WriteLine($"Modulus: {num1 % num2}"); } }
Create a program that checks if a number is even or odd using the modulus operator.
Control flow statements let you decide which blocks of code to execute and how many times.
using System; class Program { static void Main(string[] args) { // If statement int age = 18; if (age >= 18) { Console.WriteLine("You are an adult"); } // If-else statement int temperature = 25; if (temperature > 30) { Console.WriteLine("It's hot outside"); } else if (temperature > 20) { Console.WriteLine("It's warm outside"); } else { Console.WriteLine("It's cold outside"); } // Switch statement string day = "Monday"; switch (day) { case "Monday": Console.WriteLine("Start of the work week"); break; case "Friday": Console.WriteLine("Almost weekend!"); break; default: Console.WriteLine("Regular day"); break; } } }
using System; class Program { static void Main(string[] args) { // For loop for (int i = 1; i <= 5; i++) { Console.WriteLine($"Count: {i}"); } // While loop int count = 0; while (count < 3) { Console.WriteLine($"While loop iteration: {count}"); count++; } // Do-while loop int x = 5; do { Console.WriteLine($"Do-while value: {x}"); x--; } while (x > 0); // Foreach loop string[] colors = { "Red", "Green", "Blue" }; foreach (string color in colors) { Console.WriteLine($"Color: {color}"); } } }
Create a program that prints numbers from 1 to 100, but for multiples of 3 print "Fizz", for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz".
Methods are blocks of code that perform specific tasks and can be reused throughout your program.
using System; class Program { // Simple method without parameters or return value static void Greet() { Console.WriteLine("Hello, Crusader!"); } // Method with parameters static void GreetUser(string name) { Console.WriteLine($"Hello, {name}!"); } // Method with return value static int Add(int a, int b) { return a + b; } // Method with optional parameter static void DisplayInfo(string name, int age = 18) { Console.WriteLine($"Name: {name}, Age: {age}"); } // Main method - entry point of the program static void Main(string[] args) { // Calling methods Greet(); GreetUser("Alice"); int result = Add(5, 7); Console.WriteLine($"5 + 7 = {result}"); DisplayInfo("Bob"); DisplayInfo("Charlie", 25); } }
using System; class Calculator { static double Add(double a, double b) { return a + b; } static double Subtract(double a, double b) { return a - b; } static double Multiply(double a, double b) { return a * b; } static double Divide(double a, double b) { if (b == 0) { Console.WriteLine("Error: Division by zero"); return 0; } return a / b; } static void Main(string[] args) { Console.WriteLine("Simple Calculator"); Console.WriteLine("1. Add"); Console.WriteLine("2. Subtract"); Console.WriteLine("3. Multiply"); Console.WriteLine("4. Divide"); Console.Write("Enter choice (1-4): "); int choice = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter first number: "); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = Convert.ToDouble(Console.ReadLine()); double result = 0; switch (choice) { case 1: result = Add(num1, num2); break; case 2: result = Subtract(num1, num2); break; case 3: result = Multiply(num1, num2); break; case 4: result = Divide(num1, num2); break; default: Console.WriteLine("Invalid choice"); break; } Console.WriteLine($"Result: {result}"); } }
Create a program with methods to calculate the area of different shapes (circle, rectangle, triangle) and display the results.