Build essential groundwork in C#. Learn core syntax, variables, control flow, and program structure to confidently write basic C# applications.
Welcome to C#! This section will guide you through writing, compiling, and running your first C# program. We'll cover the essentials of C# syntax, project structure, and development tools.
// Example.cs
using System;
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, C# World!");
}
}
}
Let's break down the components of this program: - using System - Accesses core .NET functionality - namespace HelloWorld - Organizes code logically - class Program - Contains the application logic - Main() method - Entry point of the program
dotnet run
to build and executeA typical C# project includes: - Program.cs - Main entry file - Project file (e.g., .csproj) - Build configuration - bin/obj folders - Compiled output
// Multiple messages
Console.WriteLine("Hello, C# World!");
Console.WriteLine("Exploring the fundamentals.");
// Using variables
string message = "Welcome to C#!";
Console.WriteLine(message);
using System;
namespace CommandLineTool {
class Program {
static void Main(string[] args) {
if (args.Length == 0) {
Console.WriteLine("Please provide a command.");
return;
}
string command = args[0].ToLower();
switch(command) {
case "help":
ShowHelp();
break;
case "version":
DisplayVersion();
break;
default:
Console.WriteLine($"Unknown command: {command}");
break;
}
}
private static void ShowHelp() {
Console.WriteLine("Available commands:");
Console.WriteLine("- help: Displays this message");
Console.WriteLine("- version: Shows the current version");
}
private static void DisplayVersion() {
Console.WriteLine("Version 1.0.0");
}
}
}
This example demonstrates: - Command line argument handling - Method organization - Error checking and help functionality
Welcome to Data Types and Variables! This chapter will guide you through the core concepts of C# data handling, including value vs. reference types, type inference with `var`, and nullable types.
Variables are containers for storing data values. In C#, variables can be declared using specific data types, which determine their size and behavior.
C# has two main categories of data types: value types and reference types.
// Value type example
int number = 10;
// Reference type example
string name = "Alice";
The `var` keyword allows the compiler to infer the variable's type based on its initial assignment. Use it for clarity when the type is obvious.
// Explicit declaration
List<string> myList = new List<string>();
// Type inference with var
var myList = new List<string>();
Nullable types allow variables to hold their type's value or `null`. Use the `?` modifier after a value type.
// Nullable int
decimal? price = 29.99m;
// Check for null
if (price is not null)
{
Console.WriteLine(price.Value);
}
Use `const` for values that never change. Avoid unnecessary boxing/unboxing by choosing appropriate value types.
Control flow determines the order in which operations are performed. It's essential for creating dynamic and responsive applications. In C#, we use conditional statements, switch statements, and various loops to control program flow.
The most basic form of conditional logic is the if statement. It allows you to execute code blocks based on whether a condition evaluates to true or false.
int temperature = 25;
if (temperature > 30)
{
Console.WriteLine("It's hot outside!");
}
else if (temperature < 15)
{
Console.WriteLine("It's cold outside!");
}
else
{
Console.WriteLine("The weather is pleasant.");
}
Use the switch statement when you have multiple conditional branches based on a single value. It's more readable than long chains of if-else statements.
string day = "Monday";
switch (day)
{
case "Monday":
Console.WriteLine("Start of the workweek!");
break;
case "Friday":
Console.WriteLine("End of the workweek!");
break;
default:
Console.WriteLine("Weekend or holiday.");
break;
}
C# provides several loop constructs to repeat operations: 1. for loops - When you know how many times a loop will execute 2. while loops - When you want to check the condition before each iteration 3. do-while loops - When you want to execute at least one iteration regardless of the condition 4. foreach loops - For iterating over collections
// for loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
// while loop
int j = 0;
while (j < 5)
{
Console.WriteLine(j);
j++;
}
// do-while loop
date j = 0;
do
{
Console.WriteLine(j);
j++;
} while (j < 5);
// foreach loop
List<int> numbers = new List<int> {1, 2, 3, 4, 5};
foreach (int num in numbers)
{
Console.WriteLine(num);
}
Control flow is used in almost every part of an application. For example, user authentication systems use conditionals to check credentials, while data processing pipelines use loops and switch statements to handle different types of input.
Question 1 of 14