← C# tutorial

C#

Methods

A method declares its return type, name, and parameters up front: static int Square(int n) { ... }.

Example: a static method

using System;

class Program {
    static string Greet(string name) {
        return $"Hello, {name}!";
    }

    static void Main() {
        Console.WriteLine(Greet("Chidi"));
    }
}
Hello, Chidi!

Notice C# method names conventionally start with a capital letter (Greet, Main), this is a real, widely-followed naming convention in C# (called PascalCase), distinct from Java's convention of starting method names lowercase (greet). Same underlying concept, different community convention, worth knowing so your code reads as idiomatic C#, not just "Java syntax translated."

Example
using System;

class Program {
    static string Greet(string name) {
        return $"Hello, {name}!";
    }

    static void Main() {
        Console.WriteLine(Greet("Chidi"));
    }
}
Output
Hello, Chidi!
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account