Method Basics
A method is a named set of steps you can reuse, like a recipe card. You write it once and run it whenever you need it.
In this page:
Syntax
static ReturnType MethodName()
{
// body
}
MethodName(); // call it
Method Basics
A method is a named, reusable block of code declared with a return type, a name, and a parameter list. Calling a method transfers control to its body and resumes the caller once it returns.
Breaking logic into methods keeps programs organized and avoids repeating code.
Note:
Method names conventionally use PascalCase in C#, like
CalculateTotal.Example: Method Basics
using System;
class Program
{
static void PrintBanner()
{
Console.WriteLine("=== Welcome ===");
}
static void Main(string[] args)
{
PrintBanner();
Console.WriteLine("Program running");
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Forgetting
returnin a method with a non-void return type, which is a compile error. - Calling a non-static method from
Mainwithout creating an object. - Forgetting the parentheses when calling a method, so
Print;does not call it.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: