C# में क्लास को इंस्टेंट करने के लिए नए ऑपरेटर का उपयोग करें।
मान लीजिए कि हमारी कक्षा रेखा है। इंस्टेंटेशन एक नई वस्तु बनाएगा जैसा कि नीचे दिखाया गया है -
Line line = new Line();
ऑब्जेक्ट का उपयोग करके, अब आप विधि को कॉल कर सकते हैं -
line.setLength(6.0);
आइए उदाहरण देखें -
उदाहरण
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
} आउटपुट
Object is being created Length of line : 6