WeCodeAcademy

C# Methods

ExampleC#
using System;

class Program
{
    static int Add(int a, int b)
    {
        return a + b;
    }

    static void Greet(string name, string greeting = "Hello")
    {
        Console.WriteLine(greeting + ", " + name + "!");
    }

    static int Factorial(int n)
    {
        return n <= 1 ? 1 : n * Factorial(n - 1);
    }

    static void Main()
    {
        Console.WriteLine(Add(2, 3));
        Greet("Ada");
        Greet("Sam", "Hi");
        Console.WriteLine(Factorial(5));
    }
}
Output
5
Hello, Ada!
Hi, Sam!
120
Quick check

What does the = "Hello" in a parameter give you?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.