C# Development Mastery
C# Fundamentals
The Anatomy of a C# Program
Every C# program has a specific structure. Think of it like the grammar of a sentence. It needs certain parts in the right places to make sense. Let's look at the simplest C# program you can write, the classic "Hello, World!".
using System;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Let's break this down line by line.
using System;tells our program to include a collection of pre-written code calledSystem. This collection contains fundamental tools, including the one we use for printing text to the screen.- A
namespaceis a way to organize your code and prevent naming conflicts. We've named oursHelloWorldApp. - A
classis a container for your code's functions and data. Here, our class is namedProgram. static void Main(string[] args)is the entry point of our application. When you run the program, the computer looks for this exactMainmethod and starts executing the code inside its curly braces{}.- Finally,
Console.WriteLine("Hello, World!");is the instruction that does the work. It uses theWriteLinetool from theConsoleclass (which is part ofSystem) to print the text "Hello, World!" to the console.
Storing and Using Information
Programs need to remember things. They do this using variables. A variable is just a named storage location for a piece of data. Before you can use a variable, you have to declare it, which means giving it a name and specifying what type of data it will hold.
| Data Type | Description | Example |
|---|---|---|
int | Stores whole numbers | int age = 30; |
double | Stores numbers with decimals | double price = 19.99; |
string | Stores sequences of characters (text) | string name = "Alice"; |
bool | Stores a true or false value | bool isLoggedIn = true; |
Once you have data stored, you can interact with it. The most basic interaction is getting input from a user and showing them some output. We already saw Console.WriteLine() for output. To get input, we use Console.ReadLine().
using System;
namespace UserInput
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your name?");
string userName = Console.ReadLine();
Console.WriteLine("Hello, " + userName + "!");
}
}
}
This program prompts the user for their name, reads their input into a string variable called userName, and then uses that variable to print a personalized greeting.
Controlling the Flow
Code doesn't always run straight from top to bottom. Often, you need it to make decisions or repeat actions. This is called control flow.
The most common way to make a decision is with an if statement. It checks if a condition is true and runs a block of code only if it is. You can add an else block to run code if the condition is false.
int age = 20;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not yet an adult.");
}
To repeat actions, you use loops. A for loop is great when you know exactly how many times you want to repeat something.
// This loop will print the numbers 1 through 5
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
This loop initializes a counter variable i at 1, continues as long as i is less than or equal to 5, and adds 1 to i after each pass.
Thinking in Objects
C# is an object-oriented programming (OOP) language. This means it's designed around the concepts of "objects" and "classes." A class is like a blueprint, and an object is an actual thing built from that blueprint. For example, you could have a Car class that defines what all cars have (like wheels and a color) and what they can do (like start and stop).
// The blueprint for a Car
public class Car
{
// A property: data the object holds
public string Color { get; set; }
// A method: something the object can do
public void StartEngine()
{
Console.WriteLine("The " + Color + " car's engine starts.");
}
}
// Creating two Car objects from the class
Car myCar = new Car();
myCar.Color = "red";
myCar.StartEngine(); // Output: The red car's engine starts.
Car yourCar = new Car();
yourCar.Color = "blue";
yourCar.StartEngine(); // Output: The blue car's engine starts.
OOP is built on a few key principles:
Encapsulation: Bundling data (properties) and methods that work on that data within one unit (a class). This hides the complex details inside the class and only exposes what's necessary. In our example, someone using the
Carclass doesn't need to know how the engine starts, just that they can call theStartEngine()method.
Inheritance: Allowing a new class to take on the properties and methods of an existing class. This promotes code reuse. You could create an
ElectricCarclass that inherits fromCar. It would automatically have aColorand aStartEngine()method, but you could add new things specific to electric cars, like aChargeBattery()method.
Polymorphism: This word means "many forms." In programming, it means that a method can behave differently depending on the object that calls it. If both
Carand a newBicycleclass had aMove()method, callingMove()on aCarobject might print "Car is driving," while calling it on aBicycleobject might print "Bicycle is pedaling."
We've now covered the core building blocks of C#. With variables, control flow, and the principles of object-oriented programming, you have the foundation needed to start building more complex and powerful applications.