C# struct in hindi-C# स्त्रुक्ट क्या है?

हेल्लो दोस्तों! आज के इस पोस्ट में आपको c# struct in hindi के बारे में आपको बताया जा रहा है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

c# struct का परिचय

c# में class की तरह ही structs भी ब्लूप्रिंट(blueprint) ही होता है जिनके objects को क्रिएट किये जाते है लेकिन classes और structs में कुछ important को differences  पाए जाते है जिनके कारण इन्हे अलग अलग situations में प्रयोग किया जाता है

structs values को type होते है और classes reference type होते है structs को new keyword के साथ ही या new keyword बिना भी instantiate (object क्रिएट करना ) किया जा सकता है सभी structs directly System.valuetype से inherit होते है

structs lightweight  objects को represent करने के लिए आप उपयुक्त होते है जैसे की c# में point,rectangle और color structs को available होते है जो simple coordinates को डिक्लेअर करते है

किसी भी struct के अन्दर आप variables को initialize (value assign करवाना) नहीं कर सकते है लेकिन यदि आप variables को constant या static डिक्लेअर करते है तो उन्हे आप initialize कर सकते है

एक struct के अन्दर आप default constructor (बिना parameters वाला constructor) या finalizer नहीं डिफाइन कर सकते है लेकिन आप parameters के साथ ही constructor को डिफाइन किया जा सकता है

जब एक struct को किसी भी variables को assign किया जाता है तो उसका पूरा data उस variables में copy हो जाती है यदि उस variables में कोई change किये जाते है तो original struct को कोई भी change नहीं आता है

struct inheritance को सपोर्ट नहीं करते है यानी आप की एक struct किसी दूसरी struct या class को inherit नहीं कर सकते है इसके आलावा एक struct किसी class के लिए base के रूप में भी नहीं प्रयोग किया जा सकता है

structs को interfaces को implement कर सकते है इसके आलावा आप structs को nullable type के रूप में भी प्रयोग किया जा सकता है और null value को assign की जा सकती है

syntax of c# structs

c# में structs का general syntax आपको निचे दिया जा रही है

access-modifier struct-keyword struct-name
{
    //fields(variables),properties,methods etc here
}

struct का syntax classes की तरह ही होता है लेकिन ये classes से लिमिटेड होती है और जैसा की मैंने पहले बताया इन्हे lightweight objects को क्रिएट करने के लिए उपयोग किया जाता है

किसी भी struct का object आप 2 प्रकार से क्रिएट कर सकते है पहला तो आप new keyword  के द्वारा किसी भी struct का object को क्रिएट कर सकते है जैसे की किसी class का object क्रिएट किया जाता है

struct-name object-name=new-keyword
struct-name();

जब आप new keyword के द्वारा object को क्रिएट करते है तो object को क्रिएट हो जाता है और signature के अनुसार ही appropriate constructor कॉल हो जाता है

structs का object new keyword के बिना भी क्रिएट किया जा सकता है इस वजह से struct का allocation class से बेहतर होजाता है

struct-name object-name;

लेकिन जब आप बिना new keyword के struct का object को क्रिएट करते है तो fields initialize नहीं होते है क्योकि कोई भी constructor call नहीं होता है ऐसे में उस object को आप प्रयोग नहीं का दिया जाए fields को आप खुद struct के नाम के बाद dot(.) operator को लगाकर initialize करते है

जब आप struct को default constructor के द्वारा instantiate करते है तो सभी members को उनकी default values को assign की जाती है

example of c# structs

c# में struct का simple example आपको निचे दिया जा रहा है

using System;
public struct Employee
{
    Public string Name;
     Public int age;
     Public Employee(string n ,int a)
      {
             Name=n;
         Age=a;
      }
}
Class testEmployee
{
     Static void main()
      {
         Employee e1=new
Employee(“manoj”,40);
           Employee e2;
           Console.WriteLine(“Employee 1 name
  is {0} and age is {1}”,e1.Name,e1.Age);
             e2.Name=”ali”;
             e2.Age=30;
            Console.WriteLine(“Employee 2 name is 
{0} and age is {1}”,e2.Name,e2.Age);
          }
}

उपर दिए गए example में निचे दिए गए आउटपुट आपको निचे दिया जा रहा है

Employee 1 name is Manoj and age is ४०
Employee 2 name is ali is ३०

 

c# struct in hindi

reference-https://www.tutorialsteacher.com/csharp/csharp-struct

निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic(c# struct in hindi) या post अच्छा लगा हो तो कृपया आप इस वेबसाइट के बारे में अपने दोस्तों को जरुर बताये और -अगर कोई topic(c# struct in hindi) से संबधित प्रश्न हो तो कमेंट्स(comments) आपके लिए ही बना है और किसी Subject के लेकर भी कोई प्रश्न हो तो कमेंट करे “आपका comments हमारे लिए बहुमूल्य है “   *_*धन्यवाद

Leave a Comment