Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

methods-csharp

Methods

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Methods: the syntax, patterns, and safety habits you need before advancing in C#.

Teams still ship Methods in C# codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Methods in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Methods group reusable logic. C# supports overloading (same name, different parameters), default arguments, ref/out parameters, and expression-bodied members—features beyond plain functions in C.

Overloading and defaults

int Add(int a, int b) => a + b;
double Add(double a, double b) => a + b;

void Greet(string name = "world") {
    Console.WriteLine($"Hello, {name}");
}

static methods

static methods belong to the type, not an instance—Math.Max is a familiar example. Instance methods receive a hidden this reference like Java.

Important interview questions and answers

  1. Q: ref vs out?
    A: ref passes an initialized variable by reference; out must be assigned inside the method before return—common for TryParse patterns.
  2. Q: Expression-bodied methods?
    A: => expr syntax for single-expression members—still compile to normal methods.

Self-check

  1. Can two methods share a name if parameters differ?
  2. When would you mark a helper method static?

Pitfall: out parameters must be assigned before the method returns—compiler enforces this unlike optional Java-style patterns.

Interview prep

ref vs out?

ref passes an initialized variable by reference; out must be assigned inside the method before return—common for TryParse patterns.

Method overloading?

Same method name with different parameter lists—resolved at compile time by signature.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ref vs out?
  • params keyword?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump