Type Conversion and Casting In Hindi
C# प्रोग्रामिंग लैंग्वेज में, डाटा टाइप कन्वर्ज़न का मतलब है की किसी यूजर डिफाइन डिक्लेअर न्यूमेरिक या टेक्स्ट वेरिएबल पैरामीटर वैल्यू को एक डेटा टाइप से दूसरे डेटा टाइप में कन्वर्ट करना। C# प्रोग्रामिंग में मुख्य रूप से डाटा टाइप पैरामीटर वैल्यू कन्वर्ज़न दो प्रकार के होते हैं. जिसमे इम्प्लिसित और एक्सप्लिसित डाटा टाइप कन्वर्शन मुख्य है.

So, let’s explore the data type conversion and casting methods in C# programming.
Implicit data type conversion/type promotion in C#.
इम्प्लिसित डाटा टाइप कन्वर्ज़न C# प्रोग्रामिंग लैंग्वेज में आटोमेटिक परफॉर्म होता है, जब यूजर डिफाइन प्रोग्राम में डिक्लेअर किसी डाटा टाइप वैल्यू को स्माल डाटा टाइप से लार्ज डाटा टाइप में अपडेट या कन्वर्ट करते है. जिसमे में कोई डेटा लॉस या डाटा ओवरफ़्लो का कोई रिस्क नहीं होता है।
Examples of implicit data conversion in the C# programming language.
- छोटे से बड़े इंटीग्रल डाटा टाइप जिसमे int से long डाटा टाइप में मौजूदा डाटा वैल्यू को अपडेट या कन्वर्ट करना।
- यूजर डिक्लेअर इंटीजर डाटा टाइप पैरामीटर वैल्यू को फ़्लोटिंग डाटा पॉइंट में int से float या double डाटा टाइप में कन्वर्ट या अपडेट करना।
Example of implicit conversion.
int numeric = 77;
long bigNumeric = numeric; // here Implicit data type conversion perform with no data loss during conversion process
float decimalnumeric = numeric; // it Converts int data type into the float data type with fractional value 77.0f
Explicit data Type Conversion/Casting in C#.
C# प्रोग्रामिंग लैंग्वेज में एक्सप्लिसिट डाटा कास्टिंग या कन्वर्शन तब परफॉर्म किया जाता है, जब यूजर डिफाइन डाटा टाइप वेरिएबल पैरामीटर वैल्यू में डेटा लॉस या बड़े डाटा टाइप से छोटे डाटा टाइप में कन्वर्ज़न का रिस्क होता है।
Explicit Data Type Conversion Syntax.
targetType variableName = (targetType)sourceValue;
Explicit Data Type Example.
double numeric = 13.38;
int intNumeric = (int)numeric; // here we perform Explicit data casting it automatic truncates the decimal part
Console.WriteLine(intNumeric); // Result is – 13
Remember: यहाँ साइज में बड़े डाटा टाइप को साइज में छोटे डाटा टाइप में वेरिएबल पैरामीटर कास्ट करने से एक्चुअल डेटा टाइप वैल्यू का लॉस या डाटा वैल्यू का ओवरफ़्लो हो सकता है।
Convert using built-in methods in the C# programming language.
C# प्रोग्रामिंग लैंग्वेज में सिक्योर्ड और कंट्रोल्ड डाटा टाइप कन्वर्ज़न के लिए आपको Convert क्लास मेथड मिलती है।
Example of Convert using built-in methods.
string decimaltoString = “874”;
int output = Convert.ToInt32(decimaltoString); // here it Converts string data type to int data type
Console.WriteLine(output); // Result is – 874
Popular C# program data type convert method.
- Convert.ToInt32()
- Convert.ToDouble()
- Convert.ToBoolean()
- Convert.ToString()
Parsing string data type to numeric data type in a C# program.
C# प्रोग्रामिंग लैंग्वेज में स्ट्रिंग डाटा टाइप टेक्स्ट वैल्यू को नंबर डाटा टाइप में कन्वर्ट करने के लिए int.Parse() या double.Parse() बिल्ट इन C# फंक्शन मेथड का यूज़ किया जाता है।
Example of parsing string data type to numeric data type.
string values = “982”;
int parsedValue = int.Parse(values); // it converts string value into integer data type value
Console.WriteLine(parsedValue); // Result is – 982
Remember: यहाँ आपके पास एक स्ट्रिंग डाटा टाइप को एक वैलिड नंबर डाटा टाइप में कन्वर्ट करते समय आपके पास एक वैलिड नंबर होना चाहिए। नहीं तो यह एक प्रोग्राम एक्सेप्शन एरर को थ्रो करेगा।
Use the TryParse method for safe conversion in a C# program.
C# प्रोग्रामिंग लैंग्वेज में TryParse() फंक्शन मेथड एक्सेप्शन को रोकता है, और इंडीकेट करता है कि डाटा टाइप कन्वर्ज़न सक्सेस हुआ है, या नहीं है।
Example of the TryParse method for safe conversion.
string values = “148”;
bool succevent = int.TryParse(values, out int numeric);
if (succevent)
{
Console.WriteLine($”it Parsed the numeric value – {numeric}”);
}
else
{
Console.WriteLine(“Invalid user values enter.”);
}
Detail explanation of data Type Conversion and Casting in c#.
| Data Conversion Type | Method to use | Data type conversion Example |
| Implicit data type conversion | It performs Automatic in from small data type to large data type method | int -> long data type conversion |
| Explicit (Casting) data type conversion | Use existing c# data (type) operator or method to convert | (int)11.24 it converts int data type |
| Convert Class | Use convert class to Convert.ToInt32 to desire data type conversion method | Convert.ToDouble(“7.3”) it converts double data type |
| Parse method function | Use this method int.Parse() to convert integer value | int.Parse(“984”) it converts integer numeric values |
| TryParse method | int.TryParse() use this method function for integer conversion | int.TryParse(“492”, out p) |
C# program boxing and unboxing conversion.
- Boxing conversion – यह यूजर डिफाइन डाटा टाइप वैल्यू टाइप को ऑब्जेक्ट में कन्वर्ट करता है।
- Unboxing conversion – यह यूजर डिफाइन डाटा टाइप वैल्यू टाइप को ऑब्जेक्ट से वैल्यू टाइप में एक्सट्रेक्ट करता है.
Example of boxing and unboxing conversion.
int integer = 87;
object boxedInteger = integer; // here it converts object into the Boxing method
int unboxedInteger = (int)boxedInteger; // here it converts object into the Unboxing method
