Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 6 / 68

Writing a Method (Function)

In this lesson you will learn how to define a method (function) in C# so you can reuse the same logic over and over. This is for anyone searching for "C# method how to use".

In C#, functions are called methods. You define one in the form returnType MethodName(parameters). Bundling frequently used work under a name means you only have to call it to run it.

The sample code defines a Greet method that takes a name parameter and returns a greeting message as a string. Like a recipe, once you have named a set of steps you can reproduce the same result whenever you need it.

A common stumbling block for beginners is naming. By convention method names start with a verb, and a name like GetName() should tell you what it does; without knowing that convention, it is easy to end up with unclear names.

In real-world development, gathering shared logic into methods reduces duplication and means a fix only has to be made in one place.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)