C# type conversion in hindi-c# टाइप कन्वर्शन क्या है?

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

C# type conversion का परिचय

C# एक strongly typed language होता है C# में किसी variable में वैल्यू को स्टोर करने से पूर्व आप compile time पर  ही यह बता देते है की वह variable किस type की वैल्यू को स्टोर करेगा इसके बाद आप उस variable को किसी दूसरी type की वैल्यू को भी assign नहीं कर सकते है

example के लिए एक integer variable को declare करके आप उसे string value नहीं assign कर सकते है यह programming syntax के विरुद्ध होगा क्योकि एक integer type का structure स्ट्रिंग से अलग होता है इसे आप force करके change नहीं कर सकते है

हलाकि आप उस वैल्यू को change किया जा सकता है जो integer variable को assign की जाएगी तो example के लिए string value को इस प्रकार modify किया जा सकता है है की उसे integer variable में स्टोर किया जा सकता है यह कार्य type conversion के द्वारा किया जाता है

एक programmer के रूप में कई बार आपको किसी एक type के variable को दुसरे type का variable या value को assign करना अति आवश्यक हो सकता है ऐसी situation के लिए आप c# आपको type conversion की facility provide करती है

type conversion एक type की value को दुसरे type की वैल्यू में convert करने की process होती है

c# में type conversion 4 प्रकार की किया जा सकता है

  • implicit type conversion
  • explicit type conversion (casting)
  • user defined type conversion
  • type conversion with helper classes

इन सभी की तरह के type conversion के बारे में आगे details से बताय जा रहा  है

implicit type conversion

implicit type conversion compile के द्वारा automatically को perform किया जाता है इस तरह का type conversion built in numeric types के साथ ही संभव है जब किसी एक type की वैल्यू दुसरे type में आसानी से बिना value को reduce या cut किये fit हो सके तब ही यह conversion को compile के द्वारा perform किया जाता है

example के लिए long type का एक variable int type की कोई सी भी वैल्यू को store कर सकता है इसलिए जब आप किसी integer variable को किसी long variable को assign करते है तो compiler automatically integer variable किस value को long में convert कर देता है जिसका example आपको निचे दिया जा रहा है

using System;
class myClass
{
      Static void main(string[] args)
        {
               Long INum;
                Int Num=2474;
                Console.WriteLine(“value of Lnum is :{0}”,Num);
                INum=iNum;
                  Console.WriteLine(“value of INum is :{0}”,INum);
                }
}

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

Value of INum is 2474
Value of INum is:2474

reference types के सन्दर्भ में implicit type conversion तब होता है जब तक derived class के object को उसकी base class या interface को assign किया जाता है example के लिए यदि एक class A है जिसे class B inherit करती है तो B class का कोई भी object A class के किसी भी reference variable (not object) को assign किया जा सकता है

reference types के implicit conversion का example आपको निचे दिया जा रहा है

using System;
class myClass
{
      Public void display()
       {
                 Console.WriteLine(“hello reader..”);
        }
}
Class myOtherClass:myClass
{
}
Class myFinalClass
{
    Static void Main(string[] args)
       {
                myOtherClass dObj=new myOtherClass();
                myClass bObj=dObj;
                bObj.display();
      }
}

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

Hello Reader

implicit conversion में किसी प्रकार के special syntax की आवश्यकता नहीं होती है क्योकि conversion type safe manner में compiler के द्वारा automatically perform किया जाता है आप simply उस type के variable को assignment operator के द्वारा assign करते है और बाकी processing compiler स्वयं handle कर लेता है

जैसा की मैंने पहले ही बताया यह conversion केवल तब ही होता है जब convert होने वाली value को target type में आसानी से fit हो सके इसलिए इस तरह के conversion में किसी प्रकार का data loss भी नहीं होता है

explicit type conversion (casting)

यदि एक type से दुसरे type में implicit conversion संभव न हो तो इसके लिए आपको explicit type conversion perform करण होगा explicit type conversion को type casting भी कहा जाता है

casting के माध्यम से compiler को बताया जाता है की आप एक type की वैल्यू को दूसरी type में manually को convert करना चाहते है explicit type conversion को perform करने का general syntax आपको निचे दिया जा रहा है

(target-type) converting-variable or value;

जैसा की आप उपर दिए गए syntax में देख सकते है की जब आप किसी variable या वैल्यू को दुसरे type में cast करना चाहते है तो इसके लिए आप उस target type (जिसमे आप convert करना चाहते है ) को convert होने वाले variable या वैल्यू से पूर्व parenthesis में डिफाइन करते है इसे निचे example के द्वारा समझाया जा सकता है

using System;
class myClass
{
     Static void Main(string[] args)
       {
              Double dVar=1275.343;
             Int iVar;
             Console.WriteLine(“value of dVar is {0}”,dVar);
             iVar=(int)dVar;
             console.WriteLine(“value of dVar is {0}”,iVar);
             }
}

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

value of dVar is :1275.343
value of iVar is 1275

इसी प्रकार reference types के सन्दर्भ में भी जब आप किसी type को cast करेंगे तो इसके लिए आप target type को parenthesis में डिफाइन करेंगे example के लिए आप किसी base class के object को derived class में cast करेगे तो उससे पूर्व derived class के नाम को parenthesis में डिफाइन करेंगे इसे निचे example के द्वारा समझाया जा रहा है

Using System;
Class myClass
{
   Public void display()
      {
            Console.WriteLine(“hello reader..”);
      }
}
Class myOtherClass:myClass
{
}
Class myFinalClass
{
     Static void main(string[] args)
      {
            myOtherClass dObj=new myOtherClass();
            myClass bObj=dObj;
            myOtherClass dObj2=(myOtherClass)bObj;
             dObj2.display();
           }
}

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

Hello reader

user defined type conversion

c# में user defined type conversion special methods के द्वारा perform किया जाता है यह method user के द्वारा डिफाइन किये जाते है इस तरह का type conversion उन classes के बीच implicit और explicit type conversion के लिए होता है जिसके बीच child parent relationship नहीं होता है

user डिफाइन type conversion operator methods के द्वारा perform किया जाता है इन method को operator की तरह define किया जाता है इन method के द्वारा आप classes और structs को दूसरी classes या structs में convert कर सकते है इन operator methods से user defined type को basic types में भी convert किया जा सकता है

conversion operator को डिफाइन करने का general syntax आपको निचे दिया जा रहा है

public static implicit/explicit operator-keyword
targetTypeName(convertingTypeParameter)
{
    //conversion code here
}

जैसा की आप उपर दिए गए syntax में देख सकते है की operator मेथोड्स operator keyword के द्वारा डिफाइन किये जाते है इन method को डिफाइन करते समय सबसे पहले public access modifier को डिफाइन किया जाता है सभी operator मेथोड्स का static होना आवश्यक होता है

static keyword को डिफाइन करने के बाद implicit या explicit keyword को डिफाइन किया जाता है जिन operator method को implicit डिफाइन किया जाता है वे आवश्कतानुसार automatically एक्सीक्यूट हो जाती है जिन operator method को explicit डिफाइन किए जाता है उन्हें एक्सीक्यूट करने के लिए casting की आवश्यकता होती है

conversion operator के प्रयोग को निचे दिया example में समझाया जा रहा है

using System;
class myClass
{
     Public int Num;
       Public myClass(int n)
        {
              Num =n;
          }
      Public static implicit operator myClass(int i)
        {
                myClass temp=new myClass(i);
                     return temp;
         }
}
Class myFinalClass
{
       Static void Main(string[] args)
        {
                Int i=5;
              myClass obj=I;
              console.WriteLine(obj.Num);
          }
}

उपर दिए गए example का आउटपुट आपको generate होता है

5

type conversion with helper classes

incompatible types के बीच conversion के लिए c# आपको System.BitConverter,System.Convert या parse method को provide करती है जिनके माध्यम से आसानी से conversion को perform किया जा सकता है

datatime objects ,hexadecimal strings और byte arrays के बीच conversion के लिए System.BitConvertor और System.Convert class का प्रयोग किया जाता है parse method का प्रयोग built in numeric types के लिए किया जाता है

c# में available कुछ parse method की list आपको निचे दी जा रही है इन method में converting type को argument के रूप में pass किया जाता है

ToBoolean()Compatible type को boolean में convert करता है
ToByte()Compatible type को byte में convert करता है
ToChar()Compatible type को char में convert करता है
ToDateTime()integer या string type को DateTime structure में convert करता है
ToDecimal()Compatible type को decimal में convert करता है
ToDouble()Compatible type को double में convert करता है
ToInt16()Compatible type को 16 bit integer में convert करता है
ToInt32()Compatible type को 32 bit integer में convert करता है
ToInt64()Compatible type को 64 bit integer में convert करता है
ToSbyte()Compatible type को signed byte type में convert करता है
ToSingle()Compatible type को floating convert  करता है
ToString()Compatible type को string में convert करता है
ToType()किसी type को किसी दुसरे type में convert करता है
ToUInt16()Compatible type को 16 bit में unsigned integer में convert करता है
ToUInt32()Compatible type को 32 bit में unsigned integer में convert करता है
ToUInt64()Compatible type को 64 bit में unsigned integer में convert करता है

इन method का प्रयोग programmer के द्वारा frequently किया जाता है आपको आवश्यकतानुसार इन्हे ही अपने प्रोग्राम में प्रयोग करना चाहिए

csharp type conversion in hindi

reference-https://www.tutorialspoint.com/csharp/csharp_type_conversion.htm

c# type conversion in hindi

निवेदन-अगर आपको यह आर्टिकल(c# type conversion in hindi) अच्छा लगा हो तो आप इस पोस्ट को अपने दोस्तों के साथ जरुर शेयर(c# type conversion in hindi) करे और आपको जिस टॉपिक(c# type conversion in hindi) पर आपको पढना या नोट्स चाहिए तो हमें जरुर कमेंट करे आपका कमेंट्स हमारे लिए बहु मूल्य है धन्यवाद

Leave a Comment