← Back to C# Course | Chapter 5: Methods | Lesson 1 of 6

Method की Basics

एक method steps का एक नामित set है जिसे आप reuse कर सकते हैं, जैसे एक recipe card। आप इसे एक बार लिखते हैं और जब चाहें चलाते हैं।

In this page:

  1. Method Basics
Syntax
csharp
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

markup
using System;

class Program
{
    static void PrintBanner()
    {
        Console.WriteLine("=== Welcome ===");
    }

    static void Main(string[] args)
    {
        PrintBanner();
        Console.WriteLine("Program running");
    }
}
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. #}
आम गलतियां
  1. non-void return type वाले किसी method में return भूल जाना, जो एक compile error है।
  2. कोई object बनाए बिना Main से किसी non-static method को call करना।
  3. किसी method को call करते समय parentheses भूल जाना, जिससे Print; इसे call नहीं करता।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.