Method की Basics
एक method steps का एक नामित set है जिसे आप reuse कर सकते हैं, जैसे एक recipe card। आप इसे एक बार लिखते हैं और जब चाहें चलाते हैं।
In this page:
Syntax
static ReturnType MethodName()
{
// body
}
MethodName(); // call it
Method Basics
एक method एक नामित, reusable code का block है जो एक return type, एक नाम, और एक parameter list के साथ declared होता है। किसी method को call करना control को इसके body में transfer करता है और इसके return होने पर caller को फिर से शुरू करता है।
logic को methods में बांटना programs को organized रखता है और code दोहराने से बचाता है।
Note:
C# में method names conventionally PascalCase उपयोग करते हैं, जैसे
CalculateTotal।उदाहरण: 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. #}
आम गलतियां
- non-void return type वाले किसी method में
returnभूल जाना, जो एक compile error है। - कोई object बनाए बिना
Mainसे किसी non-static method को call करना। - किसी method को call करते समय parentheses भूल जाना, जिससे
Print;इसे call नहीं करता।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: