← Back to C# Course | Chapter 8: Interfaces | Lesson 1 of 6

Interface की Basics

एक interface promises की एक list है, जैसे 'जो भी उड़ सके उसके पास एक fly action होना चाहिए'। यह बताता है कि क्या करना है लेकिन कैसे नहीं।

In this page:

  1. Interface Basics
Syntax
csharp
interface IInterfaceName
{
    ReturnType MethodName();
}

class ClassName : IInterfaceName
{
    public ReturnType MethodName() { }
}

Interface Basics

एक interface members का एक contract declare करता है जो implementing classes को प्रदान करना चाहिए, लेकिन अपनी कोई implementation नहीं देता (default methods के अलावा)।

यह interface से declared होता है और conventionally I से शुरू होने वाला नाम रखता है, जैसे IShape। Interfaces define करते हैं कि कोई type *क्या* कर सकती है, *कैसे* नहीं।

Note: Interface members implicitly public हैं — आप उन पर कभी कोई access modifier नहीं लिखते।

उदाहरण: Interface Basics

markup
using System;

interface IGreeter
{
    void Greet();
}

class EnglishGreeter : IGreeter
{
    public void Greet()
    {
        Console.WriteLine("Hello!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IGreeter g = new EnglishGreeter();
        g.Greet();
    }
}
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. किसी interface से सीधे एक object बनाने की कोशिश करना, जो एक compile error है।
  2. किसी interface में fields जोड़ना, जिसकी अनुमति नहीं है।
  3. यह भूल जाना कि interface members default रूप से public हैं और ज़रूरत न होने पर एक access modifier जोड़ना।
🔒

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.