Wenwei Lin

← All events

Invited

MVP Tech Gathering Station · .NET C# Series 5: Create methods in C# console applications

A livestream class for C# beginners: what a method is, why you pull repeated code out, and how recursion works. With an “interview question” and three quiz questions.

Date
2023.10
Format
Microsoft Reactor
My role
Host, co-speaker
With
MVP Zhang Anzhong
Tags
Reactor · C# · .NET · programming basics

In September 2023, MVPs and student ambassadors in Greater China started a series of intro C# talks under MVP Tech Gathering Station (MVP 聚技站), the Greater China MVPs’ livestream series on Microsoft Reactor. It followed the C# course on Microsoft Learn. This was episode 5, on methods (functions). The speaker was Microsoft MVP Zhang Anzhong, who has eight years of .NET experience and co-wrote a book on ASP.NET Core 5.0. I hosted, and also shared the teaching with him: I took the most basic parts.

Opening slide: Learn Live, create methods in C# console applications

Speakers: Zhang Anzhong and Wenwei Lin

What we covered

Three parts: the basics, a code demo, and going further.

What a method is. A named block of code: call its name and the statements inside run. Starting from the simplest SayHello(), adding a parameter to get SayHello("Tom"), and following how the program jumps into the method and back.

Why pull repeated code out. The example was a small program for medicine times: the same time-formatting code was written several times; pulling it into one method made the code shorter and easier to read. Along with it, an old rule: DRY, Don’t Repeat Yourself.

DRY: don’t repeat yourself; wrap common operations into functions or methods

Treat a method as a “black box”. Only care about what goes in and what comes out; name each task first, then fill in the logic one by one.

An “interview question”. Imagine you’re in an interview and asked to write a program that checks whether an IPv4 address is valid: four numbers separated by dots, no leading zeros, each between 0 and 255. Think about how you’d break it into methods.

Coding challenge: check whether an IPv4 address is valid

Recursion. Starting from the Chinese nursery story that never ends, “Once upon a time there was a mountain, and on it a temple, where an old monk told a young monk a story…”, then Russian dolls, then comparing recursive and loop versions of factorial.

The recursion we all know: “Once upon a time there was a mountain…”

Recursion and loops: two ways to compute a factorial

Quiz. Three multiple-choice questions at the end: which line is a valid method signature, how to declare a method, how to call one. Scan to reply “.NET” and join the group to answer; prizes were a community mug and Zhang’s book.

Quiz question 1: which line is a valid method signature

Looking back

This was my first time hosting for MVP Tech Gathering Station. Half a year later I hosted an episode of its GenAI series: Managing multi-agent AI apps with .NET open source.