Starting Your Software Career with C#
C# Fundamentals
The Structure of a C# Program
Every C# application starts with an entry point. This is a special method named Main where the program's execution begins. Think of it as the front door. When your program runs, the system looks for Main and starts executing the code inside it.
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// This is the entry point of the program.
// Code execution starts here.
}
}
}
The code you write, known as source code, isn't directly understood by your computer's processor. It first needs to be compiled. The C# compiler transforms your code into Intermediate Language (IL). This IL code is then run by a virtual execution system called the Common Language Runtime (CLR), which is a core component of the .NET platform.
The CLR handles many critical tasks. It performs just-in-time (JIT) compilation, converting the IL code into native machine code that the processor can execute directly. It also manages memory automatically through a process called garbage collection, which frees up memory that is no longer in use, helping to prevent memory leaks. This managed execution environment is a key feature of C#.
Variables and Data Types
C# is a statically-typed language. This means that every variable must have a declared type, and that type is checked at compile time, not at runtime. This approach helps catch errors early in the development process. You can declare a variable by specifying its type explicitly.
// Explicitly typed variable declarations
string message = "Hello, C#!";
int score = 100;
double temperature = 21.5;
bool isComplete = false;
Alternatively, C# offers type inference using the var keyword. When you use var, the compiler determines the variable's type based on the value it's initialized with. The variable is still strongly typed; the type is just inferred for convenience.
// Inferred typing using the 'var' keyword
var inferredMessage = "This is also a string";
var inferredScore = 100; // Inferred as int
var inferredTemperature = 21.5; // Inferred as double
Using
varcan make code cleaner, especially with complex type names. However, using explicit types can make the code's intent clearer to other developers, so the choice often comes down to team preference and context.
Console Interaction
A console application is a simple way to run code and see immediate results. You can write information to the console and read input from the user. The System namespace provides a Console class for this purpose.
To display text, you use Console.WriteLine(). This method prints a string to the console and adds a new line at the end.
// Using Console.WriteLine to output text
string name = "Alice";
int level = 10;
Console.WriteLine("Welcome to the game!");
// Using string interpolation to combine variables and text
Console.WriteLine($"Player: {name}, Level: {level}");
To get input from a user, you can use Console.ReadLine(). This method pauses the program and waits for the user to type something and press Enter. It always returns the input as a string. If you need a different data type, like a number, you must convert it.
Console.WriteLine("Please enter your age:");
// ReadLine() returns a string
string input = Console.ReadLine();
// Convert the string to an integer
int age = Convert.ToInt32(input);
Console.WriteLine($"You will be {age + 1} on your next birthday.");
Now, let's test your understanding of these core concepts.
What is the name of the special method that serves as the entry point for every C# application?
The C# compiler transforms source code into an intermediate format before it is executed. What is this format called?
With this foundation, you can now start building more complex logic and exploring the wider capabilities of the C# language.
