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

Interfaces Implement करना

किसी interface को implement करने का मतलब है कि कोई class promise list पर sign करती है और फिर असल में उस पर हर action प्रदान करती है। यह किसी job candidate जैसा है जो दिखाता है कि वे ज़रूरी सभी tasks कर सकते हैं।

In this page:

  1. Implementing Interfaces
Syntax
csharp
class ClassName : IInterfaceName
{
    public ReturnType MethodName()   // every interface member needs a body
    {
    }
}

Implementing Interfaces

कोई class किसी interface को colon के बाद list करके और interface द्वारा declared हर member के लिए एक concrete body देकर इसे implement करती है। एक भी member implement न करना एक compile error है।

interface type के रूप में typed एक variable उसे implement करने वाली किसी भी class का object रख सकता है।

Warning: किसी interface के सिर्फ कुछ members implement करना compile नहीं होगा — हर member ज़रूरी है।

उदाहरण: Implementing Interfaces

markup
using System;

interface IShape
{
    double Area();
    double Perimeter();
}

class Square : IShape
{
    public double Side;
    public Square(double side) { Side = side; }

    public double Area() => Side * Side;
    public double Perimeter() => Side * 4;
}

class Program
{
    static void Main(string[] args)
    {
        IShape shape = new Square(5);
        Console.WriteLine(shape.Area());
        Console.WriteLine(shape.Perimeter());
    }
}
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 का हर member implement न करना, जो एक compile error है।
  2. किसी interface method को non-public implement करना, जिसकी implicit implementations के लिए अनुमति नहीं है।
  3. signature को थोड़ा बदलना, जिससे method interface से मेल नहीं खाता।
🔒

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.