What is Boxing, Unboxing, Delegates, Serialization and Reflection

Boxing, Unboxing, Delegates, Serialization and Reflection

Boxing और Unboxing

Boxing का अर्थ है stack में store value type(int / long / float) का heap के object type में रूपांतरण(conversion)| Unboxing का अर्थ है एक object type से वापस value type में रूपांतरण(conversion)। Boxing स्वचालित रूप से होती है, जबकि Unboxing में object reference को संगत value type में बाह्य कास्ट (explicit cast) करना होता है|

Boxing/Unboxing उदाहरण

using System;
class BoxingUnboxing
{
  static void Main()
  {
    int x=10;
    object obj=x; // Boxing
    Console.WriteLine(obj); // obj=10

    x = (int)obj; // Unboxing
    Console.WriteLine(x); //x=10
  }
}

X में value को केवल obj पर assign करके boxed किया जाता है, जबकि obj में integer value को obj की int में casting के द्वारा retrieve किया जाता है|

Delegates

Delegate एक ऐसा object है जो C या C ++ में उपयोग किए जाने वाले function pointer की तरह एक method का reference दे सकता है। इसलिए, जब आप एक Delegate बनाते हैं, तो इसका मतलब है कि आप एक ऐसे object का निर्माण कर रहे हैं जिसमे एक method का reference रखा जा सकता है। इसके अलावा, method को reference के माध्यम से call किया जा सकता है।

दूसरे शब्दों में, Delegate उस method को लागू कर सकता है, जिसे वह संदर्भित (refer) करता है।

Delegate को create करना

delegate ret-type delegateName(parameter-list); //delegate एक keyword है|


उदाहरण

delegate int Dx(int,int);

यहां Dx एक ऐसा delegate है जो किसी भी method का reference रख सकता है, जिसका return type int है और इसके 2 argument int, int हैं।

Delegate के लिए Holding method reference

delegateName objectName=new delegateName(Clsobject.MethodName);

उदाहरण


Dx odx=new Dx(oa.getdata);

यहां odx Dx delegate type का एक object है जो class A के किसी भी object oa की method का reference रखता है|

Calling Method using delegate:

delegateObject (list of arguments);

उदाहरण

int x=odx(10,20);

यहां odx (10,20) getdata(10,20) के समान है|

Serialization

Serialization object को store करने या memory, database, या file में transmit करने के लिए किसी वस्तु को stream की strema में बदलने की प्रक्रिया है। reverse process को deserialization कहा जाता है|

object को एक stream में क्रमबद्ध(serialized) किया जाता है, जो न केवल data को ले जाता है, बल्कि object के type, जैसे कि object के version, culture और assembly name के बारे में जानकारी प्राप्त करता है फिर उस stream से, इसे database, file या memory में store किया जा सकता है।

उपयोग: इसका मुख्य उद्देश्य किसी object की state को save करने के लिए है ताकि जरूरत पड़ने पर उसे फिर से बनाया जा सके। serialization के माध्यम से, एक developer Web Service के माध्यम से किसी remote application को एक domain से दूसरे domain तक object भेजने, किसी object को pass करने आदि जैसे कार्यों को कर सकता है।

Object को Serializable बनाना: किसी object को serialize करने के लिए, हमें object की आवश्यकता होती है जिसे serialize करना है, serialized objects को समाहित करने के लिए stream और System.Runtime.Serialization namespace में objects के serializing और deserializing के लिए आवश्यक classes होती हैं।

Reflection

Reflection वह विशेषता है जो आपको type के बारे में जानकारी प्राप्त करने में सक्षम बनाती है। इस जानकारी का उपयोग करके, आप runtime पर objects का निर्माण और उपयोग कर सकते हैं। यह सुविधा बहुत शक्तिशाली है क्योंकि यह निष्पादन(execution) के दौरान एक program को dynamic रूप से functionality जोड़ने देता है।

System.Reflection namespace को reflection की classes का उपयोग करने से पहले शामिल किया जाना चाहिए।

उदाहरण :-

//Reflection का उपयोग करते हुए methods का विश्लेषण करें

using System; 
using System.Reflection;
class MyClass
{ 
   Class-members
} 
class ReflectDemo
{ 
static void Main() 
     { 
MyClass obj=New MyClass(); 
Type t = typeof(obj);  // get a Type of object
 obj Console.WriteLine("Class Type of obj is: " + t.Name);
     }
}

Output: Class Type of obj is: MyClass

यहाँ typeof type object को return करता है|


error: Content is protected !!