what is interface in C# hindi?

C# Interface in Hindi

आज हम इस पोस्ट में c# interface के बारें में पढेंगे तो चलिए शुरू करते है:-

c# मे interface, class की तरह ही होता है पर फर्क सिर्फ इतना होता है कि क्लास मे जो method होता है उसका deceleration और implementation दोनों class मे ही होता है

लेकिन interface केवल method का deceleration अपने अंदर रखता है उसका implementation नहीं रखता है! तो अब आप कहोगे कि implementation कहा होगा!

जो class, interface को inherit करेगी वो class उस method का implementation करेगी!

इसका मतलब यह है कि interface केवल method का deceleration रखता है implementation नहीं रखता है! अगर कोई भी class किसी भी interface को implement करती है तो उस class की responsibility हो जाती है कि वो class उस interface के सभी method को implement करे,

अगर class, interface के किसी method को implement करने से छोड़ देती है तो program मे compile time error आ जाती है!

अतः यह compulsory होता है कि जो क्लास interface को implement करती है वो class interface के सभी methods को implement करे!

इससे यह पता चलता है कि c# interface एक contract होता है जिसे class को पूरा करना ही होता है!

आइये जानते हैं कि c# interface कैसे बनाते है

अगर आपको c# interface बनाना है तो आपको interface keyword का इस्तमाल करना होगा!

आइये Interface Declaration को एक example से समझते हैं

interface IMethod
{
int Addition (int a , int b);
}

ऊपर दिये गए example मे मैने interface keyword को use किया है और एक interface बनाया है जिसका नाम है IMethod.

c# मे जब हम interface बनाते है तब हम interface का नाम I से शुरू करते है जिससे यह पता चल जाये कि यह एक interface है! यह सिर्फ इसलिए करते है जिससे यह पता चल जाये कि यह class नहीं है यह एक interface है!

आइये अब जानते है कि एक class, interface को कैसे implement या use करती है!

Syntax for Implementation of interface:

c# interface को implement करने का syntax निम्न है:-

class <Class Name> : <Interface Name>

Implementation of Interface example:

class Calculation : IMethod

{

    public int Additon(int a , int b)

    {

      return a + b ;

    }

}

ऊपर दिये गये example मे हमने IMethod interface को Calculation class मे implement किया है!

Complete example of c# interface:

अब हम पुरे उदाहरण के द्वारा इसको देख लेते  है:-

using System;
interface IMethod
{
int Addition (int a , int b);
}
class Calculation : IMethod
{
public int Additon(int a , int b)
{
return a + b ;
}
}
class UseInterface
{
public static void Main(String [] args)
{
Calculation calObj = new Calculation();
int result = calObj.Additon(5 , 10);
Console.WriteLine(“Result of Addition is = {0} ” , result);
Console.ReadLine();
}
}

C# interface दो तरह के होता है

  1. implicit interface
  2. explicit interface

implicit interface

आइये सबसे पहले जानते है कि implicit interface क्या होता है!

आइये इसको एक example से समझते है

using System;
interface IMethod
{
 int Addition (int a , int b);
}
class Calculation : IMethod
{
 public int Additon(int a , int b)
  {
return a + b ;
 }
}
class UseInterface
{
public static void Main(String [] args)
 {
Calculation calObj = new Calculation();
int result = calObj.Additon(5 , 10);
Console.WriteLine(“Result of Addition is = {0} ” , result); 
Console.ReadLine();
}
}

ऊपर दिए गए example मे हमने IMethod interface को class मे implement किया है. IMethod interface के Additon Method को Calculation class के object से call किया है! इसको implicit interface कहते है! जब class का object interface, method को call करता है तब इसको implicit interface कहते है!

Explicit interface :

आइये अब जानते है कि explicit interface क्या होता है!

आइये इसको एक example से समझते है

using System;
interface IMethod
{
int Addition (int a , int b);
}
class Calculation : IMethod
{
public int IMethod.Additon(int a , int b) 
{
return a + b ;
}
}
class UseInterface
{
public static void Main(String [] args)
{
IMethod Obj = new Calculation();
int result = Obj.Additon(5 , 10);
Console.WriteLine(“Result of Addition is = {0} ” , result);
Console.ReadLine();
}
}

ऊपर दिया गया example explicit interface का है! explicit interface मे हम पहले interface का एक reference variable बनाते है इस reference variable मे हम class का reference pass करते है जैसा की example मे दर्शाया गया है!
IMethod Obj = new Calculation();

ऐसा करने से compiler को clear हो जाता है की यह method किस interface से implement हो रहा है!

याद रखने योग्य बातें,

  1. interface केवल methods, events, properties की deceleration को अपने अंदर रखता है!
  2. interface को आप implicit और explicit implement कर सकते हो!
  3. interface के सारे members by default public और abstract होते है!

इसे भी पढ़ें:- c# data types in hindi

निवेदन:- आपको c# interface की यह पोस्ट कैसी लगी मुझे कमेंट के द्वारा बताइए तथा इसे अपने दोस्तों के साथ share करें. धन्यवाद.

3 thoughts on “what is interface in C# hindi?”

Leave a Comment