C# & .NET Framework Methods — Your Toolbox
💡
Exercise 21

Defining Methods 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

🔧 Your Architect's Toolbox Begins!

Every master architect has a toolbox — a collection of specialized tools, each designed for a specific job. In C#, methods are your tools. Each method is a reusable block of code that performs one focused task. Instead of writing the same logic over and over, you craft a method once and call it whenever you need it.

Think of it this way: you wouldn't carry raw steel to a construction site. You'd bring a hammer. Methods are your hammers, saws, and drills — pre-built, reliable, and always ready to do their job.

📐 Anatomy of a Method

A method declaration has four essential parts:

  • Return type — What the method gives back (void means nothing).
  • Method name — A descriptive label (PascalCase by convention in C#).
  • Parameters — Inputs the method needs (inside parentheses).
  • Body — The actual code that runs, enclosed in curly braces { }.
// Syntax: // returnType MethodName(parameterList) // { // // method body // } static void SayHello() { Console.WriteLine("Hello from a method!"); }

🚫 Void Methods — The Silent Workers

A void method performs an action but doesn't return a value. It's like a drill — it does its job (makes a hole) but doesn't hand you anything back. You simply call it and it executes.

static void PrintBanner() { Console.WriteLine("=== CodeRex Construction Co. ==="); } // Calling the method: PrintBanner(); // Output: === CodeRex Construction Co. ===

⚡ Static Methods — Workspace Tools

Inside the Program class, every method you call from Main must be static. Why? Because Main itself is static — it runs without creating an object. Think of static methods as tools bolted to your workbench: they belong to the class itself, not to any specific object.

class Program { // Static method — belongs to the class static void Greet() { Console.WriteLine("Welcome, Architect!"); } static void Main() { Greet(); // Call directly — no object needed } }

🛠️ Calling Methods — Using Your Tools

To use a method, you call (or invoke) it by its name followed by parentheses. The program jumps to the method, executes its body, and returns to where it was called. It's like picking up a hammer, using it, and putting it back.

static void WelcomeMessage() { Console.WriteLine("Welcome to the workshop!"); } static void PrepareTool(string toolName) { Console.WriteLine($"Tool: {toolName} ready!"); } static void Main() { WelcomeMessage(); // Call void method PrepareTool("Hammer"); // Call with argument PrepareTool("Saw"); // Reuse the same method! Console.WriteLine("All tools initialized!"); }

💡 Key Takeaways

  • Methods are reusable blocks of code — your toolbox for clean, organized programs.
  • void methods perform actions without returning values.
  • static methods belong to the class and can be called without creating an object.
  • Method names use PascalCase in C# (e.g., CalculateArea, not calculateArea).
  • Always keep methods focused — one tool, one job.
📋 Instructions
**The Architect's First Tools** Your workshop needs its first set of tools! Create a program that: 1. Define a `static void` method called `WelcomeMessage` that prints `Welcome to the workshop!` 2. Define a `static void` method called `PrepareTool` that takes a `string toolName` parameter and prints `Tool: {toolName} ready!` 3. In `Main`, call `WelcomeMessage()`, then call `PrepareTool` twice with `"Hammer"` and `"Saw"`, then print `All tools initialized!`
Remember: methods called from Main must be static. Use `static void MethodName()` for no return, and `static void MethodName(string param)` for a parameter. Call them by name with parentheses: `WelcomeMessage();` and `PrepareTool("Hammer");`.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.