इनपुट को C# में पूर्णांक के रूप में पढ़ने के लिए Convert.ToInt32() विधि का उपयोग करें।
res = Convert.ToInt32(val);
आइए देखें कि कैसे -
Convert.ToInt32 किसी संख्या के निर्दिष्ट स्ट्रिंग प्रतिनिधित्व को एक समान 32-बिट हस्ताक्षरित पूर्णांक में परिवर्तित करता है।
सबसे पहले, कंसोल इनपुट पढ़ें -
string val; val = Console.ReadLine();
पढ़ने के बाद इसे एक पूर्णांक में बदल दें।
int res; res = Convert.ToInt32(val);
आइए एक उदाहरण देखें -
उदाहरण
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string val;
int res;
Console.WriteLine("Input from user: ");
val = Console.ReadLine();
// convert to integer
res = Convert.ToInt32(val);
// display the line
Console.WriteLine("Input = {0}", res);
}
} आउटपुट
Input from user: Input = 0
निम्नलिखित आउटपुट है। इनपुट उपयोगकर्ता द्वारा दर्ज किया जाता है।
Input from user: 2 Input = 2