जवाबों:
यदि आपको एक स्ट्रिंग मिली है, और आप यह अपेक्षा करते हैं कि यह हमेशा एक पूर्णांक हो (तो, यदि कोई वेब सेवा आपको स्ट्रिंग प्रारूप में पूर्णांक सौंप रही है), तो आप उपयोग करेंगे Int32.Parse()।
यदि आप किसी उपयोगकर्ता से इनपुट एकत्र कर रहे हैं, तो आप आम तौर पर उपयोग करेंगे Int32.TryParse(), क्योंकि यह आपको उस स्थिति पर अधिक सूक्ष्म नियंत्रण देता है, जब उपयोगकर्ता अमान्य इनपुट दर्ज करता है।
Convert.ToInt32()एक वस्तु को उसके तर्क के रूप में लेता है। (क्रिस एस का जवाब देखें कि यह कैसे काम करता है)
Convert.ToInt32()यह भी नहीं फेंकता ArgumentNullExceptionजब इसका तर्क शून्य Int32.Parse()है। यही कारण है कि यह भी मतलब है कि Convert.ToInt32()शायद एक सुबह बिट की तुलना में धीमी Int32.Parse()व्यवहार में, हालांकि, जब तक आप एक पाश में पुनरावृत्तियों की एक बहुत बड़ी संख्या कर रहे हैं, तो आप यह कभी नहीं ध्यान देंगे।
ToInt32विधि में भार के प्रकारों के लिए एक अधिभार है, इसलिए उनके बीच System.String, कोई भी समय समझदार तरीके से नहीं खोएगा। वास्तविक कोड शून्य मानों int.Parse(value, CultureInfo.CurrentCulture)के अलावा कुछ नहीं करता है, और बाकी सब चीजों के लिए।
Int32.TryParse()में Convert.ToInt32()है क्योंकि यह सही नहीं है। यदि स्ट्रिंग को गलत तरीके से स्वरूपित किया गया है, तो एक अपवाद को परिवर्तित करें।
परावर्तक में एक नजर है:
int.Parse ( "32"):
public static int Parse(string s)
{
return System.Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
जो एक कॉल है:
internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
byte* stackBuffer = stackalloc byte[1 * 0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
int num = 0;
StringToNumber(s, style, ref number, info, false);
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
if (!NumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
Convert.ToInt32 ( "32"):
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
जैसा कि पहले (डेव एम की) टिप्पणी कहती है।
Convert.ToInt32रिटर्न 0अगर एक को बढ़ाने से nullरोकने के लिए । int.ParseArgumentNullException
default(int)का संकलन समय पर किया जाता है, क्योंकि इसका आंतरिक मूल्य - अभिव्यक्ति का परिणाम है 0, इसलिए संकलक शाब्दिक रूप से सम्मिलित करता है 0। IL के डिस्सैसम उपकरण किसी भी बेहतर नहीं जान सकते, इसलिए वे आपको केवल एक शाब्दिक शून्य दिखाते हैं।
जैसे कोई अंतर नहीं। आंतरिक रूप से
Convert.ToInt32()कॉल int.Parse()करता है
एक बात के लिए छोड़कर Convert.ToInt32()रिटर्न 0जब तर्क हैnull
अन्यथा दोनों एक ही तरह से काम करते हैं
Convert.ToInt32(string)कॉल int.Parseकरता है। Convert.ToInt32(object), तथापि, कॉल ((IConvertible) value).ToInt32, जो के मामले में stringकॉल Convert.ToInt32(string)... थोड़ा घुमावदार ...
नीचे इस कोड का प्रयास करें .....
class Program
{
static void Main(string[] args)
{
string strInt = "24532";
string strNull = null;
string strWrongFrmt = "5.87";
string strAboveRange = "98765432123456";
int res;
try
{
// int.Parse() - TEST
res = int.Parse(strInt); // res = 24532
res = int.Parse(strNull); // System.ArgumentNullException
res = int.Parse(strWrongFrmt); // System.FormatException
res = int.Parse(strAboveRange); // System.OverflowException
// Convert.ToInt32(string s) - TEST
res = Convert.ToInt32(strInt); // res = 24532
res = Convert.ToInt32(strNull); // res = 0
res = Convert.ToInt32(strWrongFrmt); // System.FormatException
res = Convert.ToInt32(strAboveRange); //System.OverflowException
// int.TryParse(string s, out res) - Test
bool isParsed;
isParsed = int.TryParse(strInt, out res); // isParsed = true, res = 24532
isParsed = int.TryParse(strNull, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strWrongFrmt, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strAboveRange, out res); // isParsed = false, res = 0
}
catch(Exception e)
{
Console.WriteLine("Check this.\n" + e.Message);
}
}
अंतर यह है:
Int32.Parse()और Int32.TryParse()केवल तार बदल सकते हैं। Convert.ToInt32()किसी भी वर्ग को लागू कर सकता है IConvertible। यदि आप इसे एक स्ट्रिंग पास करते हैं, तो वे समतुल्य हैं, सिवाय इसके कि आप प्रकार की तुलनाओं के लिए अतिरिक्त ओवरहेड प्राप्त करते हैं, आदि। यदि आप स्ट्रिंग्स को परिवर्तित कर रहे हैं, तो TryParse()संभवतः बेहतर विकल्प है।
TryParse तेज है ...
इन कार्यों में से पहला, पारसे, वह है जो किसी भी .Net डेवलपर से परिचित होना चाहिए। यह फ़ंक्शन एक स्ट्रिंग लेगा और इसमें से एक पूर्णांक निकालने का प्रयास करेगा और फिर पूर्णांक लौटाएगा। यदि यह किसी ऐसी चीज में चलता है जो इसे पार्स नहीं कर सकती है तो यह एक फॉर्मेटएक्ससेप्शन फेंकता है या यदि संख्या बहुत बड़ी है तो एक ओवरफ़्लोएक्ससेप्शन। इसके अलावा, यदि आप इसे शून्य मान देते हैं तो यह एक ArgumentException को फेंक सकता है।
TryParse नए .Net 2.0 ढांचे के लिए एक नया अतिरिक्त है जो मूल पार्स फ़ंक्शन के साथ कुछ मुद्दों को संबोधित करता है। मुख्य अंतर यह है कि अपवाद हैंडलिंग बहुत धीमी है, इसलिए यदि ट्रिपपर्स स्ट्रिंग को पार्स करने में असमर्थ है, तो यह अपवाद को पार नहीं करता है जैसे कि पार्स करता है। इसके बजाय, यह एक बूलियन को संकेत देता है कि क्या वह सफलतापूर्वक एक संख्या को पार्स करने में सक्षम था। तो आपको दोनों स्ट्रिंग को पार्स करने के लिए पास करना होगा और भरने के लिए एक इंट32 आउट पैरामीटर। हम दोनों मामलों में ट्रिपरपर्स और पार्स के बीच की गति के अंतर की जांच करने के लिए प्रोफाइलर का उपयोग करेंगे, जहां स्ट्रिंग को सही ढंग से पार्स किया जा सकता है और उन मामलों में जहां स्ट्रिंग को सही ढंग से पार्स नहीं किया जा सकता है।
कन्वर्ट क्लास में एक बेस क्लास को दूसरे में बदलने के लिए कई प्रकार के फ़ंक्शन होते हैं। मेरा मानना है कि Convert.ToInt32 (स्ट्रिंग) एक अशक्त स्ट्रिंग के लिए जाँच करता है (यदि स्ट्रिंग शून्य है तो यह पार्स के विपरीत शून्य देता है) तो बस Int32.Parse (स्ट्रिंग) को कॉल करता है। मैं इस बात की पुष्टि करने के लिए और यह देखने के लिए कि क्या पार्स के विरोध के रूप में कन्वर्ट का उपयोग कर रहा हूं, प्रदर्शन पर कोई वास्तविक प्रभाव डालता है, मैं प्रोफाइलर का उपयोग करूंगा।
उम्मीद है की यह मदद करेगा।
Int32.parse (स्ट्रिंग) --->
Int32.Parse (string s) विधि संख्या के स्ट्रिंग प्रतिनिधित्व को उसके 32-बिट हस्ताक्षरित पूर्णांक समकक्ष के रूप में परिवर्तित करती है। जब s एक अशक्त संदर्भ होता है, तो यह ArgumentNullException को फेंक देगा। यदि s पूर्णांक मान से भिन्न है, तो यह FormatException को फेंक देगा। जब एस मिनल्यू से कम या मैक्सवैल्यू से अधिक संख्या का प्रतिनिधित्व करता है, तो यह ओवरफ्लो अपवाद को फेंक देगा। उदाहरण के लिए :
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
result = Int32.Parse(s1); //1234
result = Int32.Parse(s2); //FormatException
result = Int32.Parse(s3); //ArgumentNullException
result = Int32.Parse(s4); //OverflowException
Convert.ToInt32 (string) -> Convert.ToInt32 (string s) विधि 32-बिट हस्ताक्षरित पूर्णांक समकक्ष के निर्दिष्ट स्ट्रिंग प्रतिनिधित्व को परिवर्तित करती है। यह बदले में Int32.Parse () विधि कहता है। जब s एक अशक्त संदर्भ होता है, तो यह ArgumentNullException को फेंकने के बजाय 0 पर वापस आ जाएगा। यदि s पूर्णांक मान से भिन्न है, तो यह FormatException को फेंक देगा। जब एस मिनल्यू से कम या मैक्सवैल्यू से अधिक संख्या का प्रतिनिधित्व करता है, तो यह ओवरफ्लो अपवाद को फेंक देगा।
उदाहरण के लिए:
result = Convert.ToInt32(s1); // 1234
result = Convert.ToInt32(s2); // FormatException
result = Convert.ToInt32(s3); // 0
result = Convert.ToInt32(s4); // OverflowException
Convert.ToInt32
19 ओवरलोड या 19 अलग-अलग तरीके हैं जिनसे आप इसे कॉल कर सकते हैं। शायद 2010 के संस्करणों में और अधिक।
यह निम्न प्रकार से परिवर्तित करने का प्रयास करेगा;
ऑब्जेक्ट, बुलियन, चार, SByte, बाइट, Int16, UInt16, Int32, UInt32, Int64, UInt64, सिंगल, डबल, दशमलव, स्ट्रिंग, तिथि
और इसकी कई अन्य विधियां भी हैं; एक संख्या आधार के साथ करने के लिए और 2 विधियों में एक शामिल हैSystem.IFormatProvider
दूसरी ओर पार्स में केवल 4 अधिभार या 4 अलग-अलग तरीके हैं जिन्हें आप विधि कह सकते हैं।
Integer.Parse( s As String)
Integer.Parse( s As String, style As System.Globalization.NumberStyles )
Integer.Parse( s As String, provider As System.IFormatProvider )
Integer.Parse( s As String, style As System.Globalization.NumberStyles, provider As System.IFormatProvider )
यह पैरामीटर प्रकार पर निर्भर करता है। उदाहरण के लिए, मैंने आज ही पता लगाया है कि यह अपने ASCII मूल्य का उपयोग करके एक चार्ट को सीधे इंट में बदल देगा। बिल्कुल नहीं कार्यक्षमता मैं इरादा ...
आपको चेतावनी दी गई है!
public static int ToInt32(char value)
{
return (int)value;
}
Convert.ToInt32('1'); // Returns 49
int.Parse('1'); // Returns 1
charनिहित रूप से परिवर्तित कर सकते हैं string? यह निश्चित रूप से VB.NET में हो सकता है, और इसलिए उस भाषा में प्रोग्रामर की उम्मीद होगी Convert.ToInt32("1"c)और Convert.ToInt32("1")समकक्ष होने की संभावना है , लेकिन मुझे नहीं लगता कि C # में वह निहितार्थ है।
charमानों की संख्या बी.बी.नेट की तुलना में थोड़ी अधिक संख्या में है। खतरा vb.net में अधिक होगा, जहां एक निहित कास्ट के कारण वहाँ के बीच एक कथित अंतर कम है Charऔर String।
इसके लिए एक विवरण दिया गया है int.Parseऔर Convert.ToInt32: कहो, आपके पास एक चार्ट है, char[] a=['1','2','3','4']और प्रत्येक तत्व को पूर्णांक में बदलना चाहते हैं। Convert.ToInt32(a[0])आप एक नंबर 49 की यह ASCII कोड के रूप में यह व्यवहार करता है दे देंगे int.Parse(a[0])आप सही उत्पादन जो 1 है दे देंगे
यदि आपके पास एक स्ट्रिंग सरणी है string[] b=['1','2','3','4'], तो Convert.ToInt32और int.Parseआउटपुट में कोई अंतर नहीं होगा। दोनों सही पूर्णांक लौटाते हैं।
Convert.ToInt32 अशक्त मान की अनुमति देता है, यह किसी भी त्रुटि को नहीं फेंकता है। अंत में शून्य मान की अनुमति नहीं देता है, यह एक ArgumentNullException त्रुटि फेंकता है।
स्पष्टीकरण के लिए ओपन कंसोल एप्लीकेशन, बस कोड के नीचे कॉपी करें और इसे static void Main(string[] args)विधि में पेस्ट करें , मुझे आशा है कि आप समझ सकते हैं
public class Program
{
static void Main(string[] args)
{
int result;
bool status;
string s1 = "12345";
Console.WriteLine("input1:12345");
string s2 = "1234.45";
Console.WriteLine("input2:1234.45");
string s3 = null;
Console.WriteLine("input3:null");
string s4 = "1234567899012345677890123456789012345667890";
Console.WriteLine("input4:1234567899012345677890123456789012345667890");
string s5 = string.Empty;
Console.WriteLine("input5:String.Empty");
Console.WriteLine();
Console.WriteLine("--------Int.Parse Methods Outputs-------------");
try
{
result = int.Parse(s1);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:"+ee.Message);
}
try
{
result = int.Parse(s2);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
result = int.Parse(s3);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
result = int.Parse(s4);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
result = int.Parse(s5);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.WriteLine();
Console.WriteLine("--------Convert.To.Int32 Method Outputs-------------");
try
{
result= Convert.ToInt32(s1);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:" + ee.Message);
}
try
{
result = Convert.ToInt32(s2);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
result = Convert.ToInt32(s3);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
result = Convert.ToInt32(s4);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
result = Convert.ToInt32(s5);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.WriteLine();
Console.WriteLine("--------TryParse Methods Outputs-------------");
try
{
status = int.TryParse(s1, out result);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:" + ee.Message);
}
try
{
status = int.TryParse(s2, out result);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
status = int.TryParse(s3, out result);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
status = int.TryParse(s4, out result);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
status = int.TryParse(s5, out result);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.Read();
}
}
Parse () विधियाँ उन संख्या शैलियों को प्रदान करती हैं जिनका उपयोग Convert () के लिए नहीं किया जा सकता है। उदाहरण के लिए:
int i;
bool b = int.TryParse( "123-",
System.Globalization.NumberStyles.AllowTrailingSign,
System.Globalization.CultureInfo.InvariantCulture,
out i);
ट्रेलिंग साइन के साथ संख्याओं को पार्स करेगा ताकि i == -123
ट्रेलिंग साइन ईआरपी सिस्टम में लोकप्रिय हो।