Build a strong foundation in C# programming and .NET ecosystem.
Build a strong foundation in C# programming and .NET ecosystem.
Learn the basic syntax and structure of the C# programming language
Content by: Sunny Radadiya
.Net Developer
C# (pronounced 'C Sharp') is a modern, object-oriented programming language developed by Microsoft. It's part of the .NET ecosystem and is widely used for building Windows applications, web applications, mobile apps, and cloud services.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C# Programming!");
// Get user input
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
}
}
}
// Comments in C#
// Single-line comment
/* Multi-line
comment */
// Namespace declaration
using System;
// Class declaration
public class MyClass
{
// Method declaration
public void MyMethod()
{
// Code goes here
}
}
// Variable declaration
int age = 25;
string name = "John";
bool isStudent = true;
// Console output
Console.WriteLine("Hello, World!");
Console.Write("Enter your name: ");
// Console input
string input = Console.ReadLine();
Test your understanding of this topic:
Understand the .NET ecosystem, runtime, and framework components
Content by: Akash Vadher
.Net Developer
.NET is a free, cross-platform, open-source developer platform for building many different types of applications. It provides a consistent development experience across different platforms and programming languages.
Evolution of .NET:
.NET Framework (2002-2022)
- Windows-only
- Proprietary
- Mature and stable
- Legacy applications
.NET Core (2016-2020)
- Cross-platform
- Open-source
- High performance
- Cloud-optimized
.NET 5+ (2020-Present)
- Unified platform
- Cross-platform
- Open-source
- Modern development
Current Version: .NET 8 (LTS)
- Released: November 2023
- Long-term support
- Performance improvements
- New features and APIs
// Create a new console application
dotnet new console -n MyFirstApp
// Navigate to project directory
cd MyFirstApp
// Run the application
dotnet run
// Build the application
dotnet build
// Publish the application
dotnet publish -c Release
// Add a package
dotnet add package Newtonsoft.Json
// Restore packages
dotnet restore
Test your understanding of this topic:
Master C# data types, variable declaration, and operators for effective programming
Content by: Vaibhav Nakrani
.Net Developer
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.
// 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);
// 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;
// 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;
// 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
Test your understanding of this topic:
Learn to control program execution with conditional statements and loops
Content by: Dipen Dalvadi
.Net Developer
Control flow statements allow your program to make decisions and execute different code blocks based on conditions.
// Basic if statement
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
// If-else statement
int score = 85;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
Console.WriteLine("Grade: C");
}
else
{
Console.WriteLine("Grade: F");
}
// Ternary operator
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine($"Status: {status}");
// Switch statement
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
Console.WriteLine("Good job!");
break;
case 'C':
Console.WriteLine("Satisfactory");
break;
case 'D':
Console.WriteLine("Needs improvement");
break;
case 'F':
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
// Switch expression (C# 8.0+)
string message = grade switch
{
'A' => "Excellent!",
'B' => "Good job!",
'C' => "Satisfactory",
'D' => "Needs improvement",
'F' => "Failed",
_ => "Invalid grade"
};
// For loop
Console.WriteLine("Counting from 1 to 5:");
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Count: {i}");
}
// While loop
int count = 0;
while (count < 3)
{
Console.WriteLine($"While loop: {count}");
count++;
}
// Do-while loop
int number;
do
{
Console.Write("Enter a positive number: ");
number = int.Parse(Console.ReadLine());
} while (number <= 0);
// Foreach loop
string[] fruits = { "Apple", "Banana", "Orange" };
foreach (string fruit in fruits)
{
Console.WriteLine($"Fruit: {fruit}");
}
// Break statement
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
Console.WriteLine("Breaking at 5");
break; // Exit the loop
}
Console.WriteLine(i);
}
// Continue statement
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
Console.WriteLine("Skipping 3");
continue; // Skip to next iteration
}
Console.WriteLine(i);
}
// Nested loops
for (int i = 1; i <= 3; i++)
{
Console.WriteLine($"Outer loop: {i}");
for (int j = 1; j <= 2; j++)
{
Console.WriteLine($" Inner loop: {j}");
}
}
Test your understanding of this topic:
Learn to create and use methods to organize and reuse code effectively
Content by: Sunny Radadiya
.Net Developer
Methods are blocks of code that perform specific tasks. They help organize code, make it reusable, and improve readability. In C#, methods are defined within classes.
// Method declaration syntax
[access_modifier] [static] return_type MethodName(parameters)
{
// method body
return value; // if return_type is not void
}
// Example method
public static int Add(int a, int b)
{
int result = a + b;
return result;
}
// Method with no return value
public static void PrintMessage(string message)
{
Console.WriteLine(message);
}
// Method with no parameters
public static void SayHello()
{
Console.WriteLine("Hello, World!");
}
// Value parameters
public static int Multiply(int x, int y)
{
return x * y;
}
// Reference parameters (ref)
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
// Output parameters (out)
public static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
// Params array
public static int Sum(params int[] numbers)
{
int total = 0;
foreach (int num in numbers)
{
total += num;
}
return total;
}
// Optional parameters with default values
public static void Greet(string name, string greeting = "Hello")
{
Console.WriteLine($"{greeting}, {name}!");
}
// Method overloading - same name, different parameters
public static int Add(int a, int b)
{
return a + b;
}
public static double Add(double a, double b)
{
return a + b;
}
public static int Add(int a, int b, int c)
{
return a + b + c;
}
// Usage
int sum1 = Add(5, 10); // Calls first method
double sum2 = Add(5.5, 10.5); // Calls second method
int sum3 = Add(5, 10, 15); // Calls third method
// Recursive method - calls itself
public static int Factorial(int n)
{
if (n <= 1)
return 1;
else
return n * Factorial(n - 1);
}
// Fibonacci sequence
public static int Fibonacci(int n)
{
if (n <= 1)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
// Usage
int fact = Factorial(5); // 120
int fib = Fibonacci(10); // 55
Test your understanding of this topic:
Introduction to OOP concepts: classes, objects, encapsulation, and basic inheritance
Content by: Akash Vadher
.Net Developer
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects that contain both data (attributes) and behavior (methods). C# is an object-oriented language.
// Class definition
public class Person
{
// Fields (attributes)
public string Name;
public int Age;
public string Email;
// Constructor
public Person(string name, int age, string email)
{
Name = name;
Age = age;
Email = email;
}
// Methods (behavior)
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
public void SendEmail(string message)
{
Console.WriteLine($"Sending email to {Email}: {message}");
}
}
// Creating objects
Person person1 = new Person("John", 25, "john@email.com");
Person person2 = new Person("Jane", 30, "jane@email.com");
// Using objects
person1.Introduce();
person2.SendEmail("Hello from C#!");
// Encapsulation - controlling access to data
public class BankAccount
{
// Private fields (encapsulated)
private decimal balance;
private string accountNumber;
// Public properties (controlled access)
public decimal Balance
{
get { return balance; }
private set { balance = value; }
}
public string AccountNumber
{
get { return accountNumber; }
}
// Constructor
public BankAccount(string accountNumber, decimal initialBalance)
{
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Public methods
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
Console.WriteLine($"Deposited {amount:C}. New balance: {balance:C}");
}
}
public bool Withdraw(decimal amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
Console.WriteLine($"Withdrew {amount:C}. New balance: {balance:C}");
return true;
}
Console.WriteLine("Insufficient funds or invalid amount.");
return false;
}
}
// Base class
public class Vehicle
{
public string Brand { get; set; }
public int Year { get; set; }
public int Speed { get; set; }
public Vehicle(string brand, int year)
{
Brand = brand;
Year = year;
Speed = 0;
}
public virtual void Start()
{
Console.WriteLine($"{Brand} vehicle started.");
}
public void Accelerate(int increment)
{
Speed += increment;
Console.WriteLine($"Speed increased to {Speed} km/h");
}
}
// Derived class
public class Car : Vehicle
{
public int Doors { get; set; }
public Car(string brand, int year, int doors) : base(brand, year)
{
Doors = doors;
}
public override void Start()
{
Console.WriteLine($"{Brand} car with {Doors} doors started.");
}
public void OpenTrunk()
{
Console.WriteLine("Trunk opened.");
}
}
// Usage
Car myCar = new Car("Toyota", 2023, 4);
myCar.Start();
myCar.Accelerate(50);
myCar.OpenTrunk();
Test your understanding of this topic:
Continue your learning journey and master the next set of concepts.
Continue to Module 2