इस ट्यूटोरियल में, हम सीखेंगे कि उपयोगकर्ता से इनपुट कैसे लें और विभिन्न तरीकों का उपयोग करके C # में आउटपुट प्रदर्शित करें
सी # आउटपुट
C # में कुछ आउटपुट करने के लिए, हम उपयोग कर सकते हैं
System.Console.WriteLine () या System.Console.Write ()
इधर, System
एक नाम का स्थान Console
नाम स्थान के भीतर एक वर्ग है System
और WriteLine
और Write
वर्ग के तरीके हैं Console
।
आइए एक सरल उदाहरण देखें जो एक स्ट्रिंग को आउटपुट स्क्रीन पर प्रिंट करता है।
उदाहरण 1: राइटिंगलाइन () का उपयोग करते हुए प्रिंटिंग स्ट्रिंग
using System; namespace Sample ( class Test ( public static void Main(string() args) ( Console.WriteLine("C# is cool"); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
सी # शांत है
WriteLine () और Write () विधि के बीच अंतर
के बीच मुख्य अंतर WriteLine()
और Write()
यह है कि Write()
विधि केवल इसे प्रदान की गई स्ट्रिंग को प्रिंट करती है, जबकि WriteLine()
विधि स्ट्रिंग को प्रिंट करती है और अगली पंक्ति के शुरू होने के साथ-साथ चलती है।
आइए इन तरीकों के अंतर को समझने के लिए नीचे दिए गए उदाहरण पर एक नज़र डालें।
उदाहरण 2: राइटलाइन () और राइट () विधि का उपयोग कैसे करें?
using System; namespace Sample ( class Test ( public static void Main(string() args) ( Console.WriteLine("Prints on "); Console.WriteLine("New line"); Console.Write("Prints on "); Console.Write("Same line"); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
नई लाइन पर प्रिंट समान लाइन पर प्रिंट
वैरिएबल और लिटरल प्रिंटिंग राइटलाइन () और राइट () का उपयोग कर
WriteLine()
और Write()
विधि चर और शाब्दिक मुद्रित करने के लिए इस्तेमाल किया जा सकता। यहाँ एक उदाहरण है।
उदाहरण 3: मुद्रण चर और साहित्य
using System; namespace Sample ( class Test ( public static void Main(string() args) ( int value = 10; // Variable Console.WriteLine(value); // Literal Console.WriteLine(50.05); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
10 50.05
+ (ऑपरेटर) का उपयोग करके और उनका मुद्रण करते हुए दो तारों को मिलाते (समेटते हुए)
+
प्रिंटिंग करते समय ऑपरेटर का उपयोग करके स्ट्रिंग्स को जोड़ा जा सकता है ।
उदाहरण 4: मुद्रण संकेंद्रित स्ट्रिंग + ऑपरेटर का उपयोग करके
using System; namespace Sample ( class Test ( public static void Main(string() args) ( int val = 55; Console.WriteLine("Hello " + "World"); Console.WriteLine("Value = " + val); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
हैलो वर्ल्ड वैल्यू = 55
स्वरूपित स्ट्रिंग (बेहतर वैकल्पिक) का उपयोग करके समतल स्ट्रिंग को मुद्रण करना
समवर्ती स्ट्रिंग छपाई के लिए एक बेहतर विकल्प स्वरूपित स्ट्रिंग का उपयोग कर रहा है। स्वरूपित स्ट्रिंग प्रोग्रामर को चर के लिए प्लेसहोल्डर का उपयोग करने की अनुमति देता है। उदाहरण के लिए,
निम्न पंक्ति,
Console.WriteLine ("मान =" + वैल);
द्वारा प्रतिस्थापित किया जा सकता है,
Console.WriteLine ("मान = (0)", वैल);
(0)
वैरिएबल वैल के लिए प्लेसहोल्डर है जिसे वैल के वैल द्वारा बदल दिया जाएगा। चूंकि केवल एक चर का उपयोग किया जाता है, इसलिए केवल एक ही स्थान है।
एकाधिक चर का उपयोग स्वरूपित स्ट्रिंग में किया जा सकता है। हम नीचे दिए गए उदाहरण में देखेंगे।
उदाहरण 5: स्ट्रिंग प्रारूपण का उपयोग करके समाप्त स्ट्रिंग को प्रिंट करना
using System; namespace Sample ( class Test ( public static void Main(string() args) ( int firstNumber = 5, secondNumber = 10, result; result = firstNumber + secondNumber; Console.WriteLine("(0) + (1) = (2)", firstNumber, secondNumber, result); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
5 + 10 = 15
यहाँ, पहले (0)
नम्बर द्वारा प्रतिस्थापित किया जाता है, (1)
दूसरी निनंबर द्वारा प्रतिस्थापित किया जाता है और (2)
परिणाम द्वारा प्रतिस्थापित किया जाता है। मुद्रण आउटपुट का यह तरीका +
ऑपरेटर के उपयोग की तुलना में अधिक पठनीय और कम त्रुटि वाला है ।
स्ट्रिंग प्रारूपण के बारे में अधिक जानने के लिए, C # स्ट्रिंग प्रारूपण पर जाएँ।
सी # इनपुट
C # में, उपयोगकर्ता से इनपुट प्राप्त करने का सबसे सरल तरीका वर्ग ReadLine()
विधि का उपयोग करके है Console
। हालाँकि, Read()
और ReadKey()
उपयोगकर्ता से इनपुट प्राप्त करने के लिए भी उपलब्ध हैं। उन्हें Console
क्लास में भी शामिल किया जाता है ।
उदाहरण 6: उपयोगकर्ता से स्ट्रिंग इनपुट प्राप्त करें
using System; namespace Sample ( class Test ( public static void Main(string() args) ( string testString; Console.Write("Enter a string - "); testString = Console.ReadLine(); Console.WriteLine("You entered '(0)'", testString); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा:
एक स्ट्रिंग दर्ज करें - हैलो वर्ल्ड आपने 'हैलो वर्ल्ड' में प्रवेश किया
ReadLine (), Read () और ReadKey () विधि के बीच अंतर:
के बीच का अंतर ReadLine()
, Read()
और ReadKey()
विधि है:
ReadLine()
: TheReadLine()
method reads the next line of input from the standard input stream. It returns the same string.Read()
: TheRead()
method reads the next character from the standard input stream. It returns the ascii value of the character.ReadKey()
: TheReadKey()
method obtains the next key pressed by user. This method is usually used to hold the screen until user press a key.
If you want to know more about these methods, here is an interesting discussion on StackOverflow on: Difference between Console.Read() and Console.ReadLine()?.
Example 7: Difference between Read() and ReadKey() method
using System; namespace Sample ( class Test ( public static void Main(string() args) ( int userInput; Console.WriteLine("Press any key to continue… "); Console.ReadKey(); Console.WriteLine(); Console.Write("Input using Read() - "); userInput = Console.Read(); Console.WriteLine("Ascii Value = (0)",userInput); ) ) )
When we run the program, the output will be
Press any key to continue… x Input using Read() - Learning C# Ascii Value = 76
From this example, it must be clear how ReadKey()
and Read()
method works. While using ReadKey()
, as soon as the key is pressed, it is displayed on the screen.
When Read()
is used, it takes a whole line but only returns the ASCII value of first character. Hence, 76
(ASCII value of L
) is printed.
Reading numeric values (integer and floating point types)
Reading a character or string is very simple in C#. All you need to do is call the corresponding methods as required.
But, reading numeric values can be slightly tricky in C#. We’ll still use the same ReadLine()
method we used for getting string values. But since the ReadLine()
method receives the input as string, it needs to be converted into integer or floating point type.
हमारे इनपुट को परिवर्तित करने के लिए एक सरल दृष्टिकोण Convert
कक्षा के तरीकों का उपयोग कर रहा है ।
उदाहरण 8: कन्वर्ट क्लास का उपयोग करते हुए उपयोगकर्ता से संख्यात्मक मान पढ़ना
using System; namespace UserInput ( class MyClass ( public static void Main(string() args) ( string userInput; int intVal; double doubleVal; Console.Write("Enter integer value: "); userInput = Console.ReadLine(); /* Converts to integer type */ intVal = Convert.ToInt32(userInput); Console.WriteLine("You entered (0)",intVal); Console.Write("Enter double value: "); userInput = Console.ReadLine(); /* Converts to double type */ doubleVal = Convert.ToDouble(userInput); Console.WriteLine("You entered (0)",doubleVal); ) ) )
जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा
पूर्णांक मान दर्ज करें: 101 आपने 101 दर्ज किया डबल मान दर्ज करें: 59.412 आपने 59.412 में प्रवेश किया
ToInt32()
और ToDouble()
Convert वर्ग की विधि क्रमशः पूर्णांक और डबल प्रकार के लिए स्ट्रिंग इनपुट बदल देता है। इसी प्रकार हम इनपुट को अन्य प्रकारों में बदल सकते हैं। यहां कन्वर्ट क्लास के लिए उपलब्ध तरीकों की पूरी सूची दी गई है।
उपयोगकर्ता से संख्यात्मक इनपुट प्राप्त करने के अन्य तरीके हैं। अधिक जानने के लिए, उपयोगकर्ता इनपुट से पूर्णांक पढ़ना पढ़ना।