ASP.Net में क्लास और इंटरफ़ेस

क्लास का परिचय (Introduction to Class)

Class के लिए class keyword का उपयोग करके declare किया जाता है , class का अपना एक नाम होता है एवं class के members curly braces के बीच घिरे हुए होते है| हर class का एक constructor होता है| जब भी class का instance create होता है तब यह constructor स्वतः call होता है | constructor के पास return value नहीं होती, और constructor का नाम हमेशा class के नाम के समान होता है|

C# Classes का उदाहरण: Classes.cs

// Namespace Declaration
using System;
class OutputClass
{
string myString;
// Constructor
public OutputClass(string inputString)
{
myString = inputString;
}
// Instance Method
public void printString()
{
Console.WriteLine("{0}", myString);
}
// Destructor
~OutputClass()
{
// Some resource cleanup routines
}
}
// Program start class
class ExampleClass
{
// Main begins program execution.
public static void Main()
{
// Instance of OutputClass
OutputClass outCl = new OutputClass("This is printed by the output class.");
// Call Output class' method
outCl.printString();
}
}

Output में एक constructor, instance method और एक destructor है। इसके पास myString नाम की एक field भी है|

इंटरफ़ेस का परिचय (Introduction to Interfaces)

Interface एक class की तरह दिखता है, लेकिन interface का कोई implementation नहीं होता| interface में केवल events, methods और properties का declaration शामिल हैं। Interface को केवल classes द्वारा ही inherit किया जा सकता है, एवं इसमें प्रत्येक interface member का implementation शामिल होता है|

Interface को परिभाषित करना

interface MyInterface
{
void MethodToImplement();
}

इस method का कोई implementation नहीं है क्योंकि interface केवल उन methods को निर्दिष्ट(specify) करता है जिन्हें class में implement करना चाहिए।

Interface का उपयोग करना


class UseInterface : MyInterface
{
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
static void Main()
{
UseInterface oi = new UseInterface();
oi.MethodToImplement();
}
}

MyInterface Interface को UseInterface class में implement किया गया है। एक Interface अन्य interface को भी implement कर सकता है|


error: Content is protected !!