C# Console.WriteLine | C# Console.ReadLine

C# Console.WriteLine | C# Console.ReadLine: Console I/O को Console.In, Console.Out व Console.Error नाम के Standard Streams द्वारा Accomplish किया जाता है। इससे पहले कि हम आगे बढें, ये समझ लेना जरूरी है कि ज्यादातर Professional Applications Console Mode नहीं होते, बल्कि Window Based GUI Applications होते हैं अथवा Server Side Codes होते हैं।

अत: यहां हम जिन Concepts को Describe करेंगे, उन्हें Professionally बहुत कम उपयोग में लिया जाता है। लेकिन I/O की Internal Working को बेहतर तरीके से समझने के लिए ये Console Mode Applications काफी उपयोगी साबित होते हैं।

Reading Console Input

Console.In वास्तव में TextReader Type का एक Instance है और हम TextReader Class के विभिन्न Methods व Proprieties का प्रयोग इस Instance के साथ कर सकते हैं। हालांकि हम Console.In के स्थान पर सामान्‍यत: Console द्वारा Provided Methods को ही Use करते हैं जो कि Automatically Internally Console.In से ही Data की Reading करता है। Console में मुख्‍य रूप से तीन Methods Read(), ReadLine() व ReadKey() को Define किया गया है, जो कि .NET Framework 2.0 से Exist हैं।

Read() Method का प्रयोग Keyboard से एक Single Character Read करने के लिए किया जाता है। ये एक Static Method है, जो Console से Read होने वाले Next Character के Integer मान को Return करता है। ये Method तब तक Wait करता है, जब तक कि User कोई Key Press करके Enter Key को Press नहीं करता।

Input किया जाने वाला Character एक Integer Value के रूप में Return होता है, जिसे हमें Character की तरह प्राप्त करने के लिए char Type में Cast करना पडता है। जबकि Error की स्थिति में Read() Method -1 Return करता है तथा Failure की स्थिति में IOException Throw करता है।

जब हम Read() Method को Use करते हैं, तो Console Input Line-Buffered होता है। अत: हमें Input किए गए Character को Program में Send करने के लिए Enter Key को Press करना जरूरी होता है। इसे समझने के लिए हम निम्नानुसार एक Program Create कर सकते हैं:

File Name: ReadSingleCharacterFromInputDevice.cs
using System.IO;
using System;

namespace CSharpFilesAndStreams
{
    class KbIn
    {
        static void Main()
        {
            char ch;
            Console.Write("Press a key followed by ENTER: ");
            ch = (char)Console.Read(); 				// get a char
            Console.WriteLine("Your key is: " + ch);
        }
    }
}

// Output
   Press a key followed by ENTER: Kuldeep
   Your key is: K

चूंकि Read() Method एक बार में केवल एक ही Character Read करता है, इसीलिए जैसाकि हम Output में देख सकते हैं कि हालांकि हमने Console पर पूरा नाम Input किया है, फिर भी Output में केवल एक ही Character Return हो रहा है।

चूंकि Read() Method Line-Buffered होता है। इसलिए Character Type करने के बाद जब तक हम Enter Key Press नहीं करते, Character हमारे Program में नहीं पहुंचता। लेकिन Line-Buffered होने की वजह से हम जो Enter Key Press करते हैं, उसका Code (Carriage-Return and Line-Feed), Buffer में ही पडा रहता है। अत: कुछ Applications में जहां हमें Enter Key के Code से परेशानी हो रही होती है, वहां इन्हें Reading से Remove करने के लिए हम ReadKey() Method को Use करते हैं।

जब हमें Input Device से पूरी Text Line को एक ही बार में Read करना होता है, तब हम ReadLine() Method को Use करते हैं। ये Method भी एक Static Method है जो कि Read की जाने वाली Text Line को एक String Object के रूप में Return करता है। ये Method तब तक Characters को Read करता रहता है, जब तक कि हम Enter Key Press नहीं करते। जबकि Error की स्थिति में ये Method भी IOException Throw करता है। इस Method को हम निम्नानुसार Use कर सकते हैं:

File Name: ReadLineOfTextFromInputDevice.cs
using System.IO;
using System;

namespace CSharpFilesAndStreams
{
    class ReadString
    {
        static void Main()
        {
            string str;
            Console.Write("Enter some characters.");
            str = Console.ReadLine();
            Console.WriteLine("You entered: " + str);
        }
    }
}

// Output
   Enter some characters. Kuldeep
   You entered: Kuldeep

हालांकि Console Methods का प्रयोग करके हम Single Character या Text Line को आसानी से Read कर सकते हैं। लेकिन हम वास्तव में Internally TextReader Class के Methods को ही Call कर रहे होते हैं। उदाहरण के लिए यदि हम हमारे पिछले उदाहरण को Console.In का प्रयोग करते हुए Re-Write करें, तो हमारा Program कुछ निम्नानुसार होता है:

File Name: ReadLineOfTextFromInputDevice.cs
using System.IO;
using System;

namespace CSharpFilesAndStreams
{
    class ReadChars2
    {
        static void Main()
        {
            string str;
            Console.Write("Enter some characters.");
            str = Console.In.ReadLine(); 		// call TextReader's ReadLine() method
            Console.WriteLine("You entered: " + str);
        }
    }
}

// Output
   Enter some characters. Kuldeep
   You entered: Kuldeep

Using ReadKey()

ReadKey() की ये विशेषता है कि इसका प्रयोग करने पर ये Method तभी तक Wait करता है, जब तक User कोई Key Press नहीं कर देता। जैसे ही User कोई Key Press करता है, ये Method Run हो जाता है और Enter Key Press करने का Wait नहीं करता। इसलिए इस Method को Read() Method के Alternative के रूप में तब Use किया जाता है, जब हमें Line-Buffered Enter Key के कारण हमारे Console Application में कोई परेशानी आ रही होती है। इस Method को दो तरीकों से Use किया जा सकता है:

static ConsoleKeyInfo ReadKey( )
static ConsoleKeyInfo ReadKey(bool intercept)

जब हम पहले तरीके से ReadKey() को Use करते हैं, तो Keyboard पर कोई Key Press करते ही ये Method Return हो जाता है। साथ ही Input किया गया Character Screen पर Display भी होता है।

लेकिन जब हम दूसरे तरीके से इसे Use करते हुए intercept Parameter के रूप में true Specify करते हैं, तो Input किया जाने वाला Character Screen पर Display नहीं होताए जबकि इसका Default मान false होता है और false मान की स्थिति में ये Method पहले वाले ReadKey() Method की तरह ही Input किए गए Character को Screen पर Display करता है।

ये Method Input किए गए Character को एक ConsoleKeyInfo Type के Object के रूप में Return करता है, जो कि एक Structure होता है और इसकी निम्नानुसार तीन Read-Only Properties होती हैं:

char KeyChar
ConsoleKey Key
ConsoleModifiers Modifiers

KeyChar Property में Press किए गए Character का Equivalent Character होता है। जबकि Key Property में ConsoleKey Enum Type में Specified कोई Enum मान होता है। इस Enum Type में Keyboard के सभी Characters का एक Enum Constant होता है।

Modifiers के रूप में Alt, Ctrl या Shift की Information होती है। यानी यदि Key Press करते समय इन में से किसी को Press किया गया हो, तो Modifiers Property में Press की गई इन Special Keys की Information होती है। इन Modifiers को भी ConsoleModifiers नाम के Enum Type द्वारा Control, Shift Alt Value के रूप में Represent किया जाता है।

इन विभिन्न Properties के साथ यदि हम ReadKey() Method को Use करना चाहें, तो हम निम्नानुसार एक Example Program Create कर सकते हैं:

File Name: ReadKeyOfTextFromInputDevice.cs
using System.IO;
using System;

namespace CSharpFilesAndStreams
{
    class ReadKeys
    {
        static void Main()
        {
            ConsoleKeyInfo keypress;
            Console.WriteLine("Enter keystrokes. Enter Q to stop.");

            do
            {
                keypress = Console.ReadKey(); // read keystrokes
                Console.WriteLine(" Your key is: " + keypress.KeyChar);

                // Check for modifier keys.
                if ((ConsoleModifiers.Alt & keypress.Modifiers) != 0)
                    Console.WriteLine("Alt key pressed.");

                if ((ConsoleModifiers.Control & keypress.Modifiers) != 0)
                    Console.WriteLine("Control key pressed.");

                if ((ConsoleModifiers.Shift & keypress.Modifiers) != 0)
                    Console.WriteLine("Shift key pressed.");
            } while (keypress.KeyChar != 'Q');
        }
    }
}

// Output
   Enter keystrokes. Enter Q to stop.
   a Your key is: a
   Your key is:
   Alt key pressed.
   Control key pressed.
   O Your key is: O
   Shift key pressed.
   q Your key is: q
   Q Your key is: Q
   Shift key pressed.

Writing Console Output

Console.OutConsole.Error दोनों TextWriter Type के Object हैं। Output को Console पर Display करने के लिए हम Write() व WriteLine() Methods को इस पुस्तक के कई Programs में Use कर चुके हैं। जहां Write() Method Automatically New Line नहीं लेता जबकि WriteLine() Method Automatically एक New Line ले लेता है।

इसी तरह से इन Methods के और भी बहुत सारे Overloaded Versions हैं, जिनके बारे में Detailed Information प्राप्त करने के लिए Visual Studio के Object Browser Tool को Use किया जा सकता है।

हालांकि हमने हमारे विभिन्न Programs में Console.Out के Alternative Method को बहुत Use किया है। लेकिन यदि हम चाहें तो हम Console.Out के Alternate के रूप में Console.Error को भी Use कर सकते हैं क्योंकि Default रूप से ये दोनों Console पर ही Text को Write करते हैं। जैसे:

File Name: WritingConsoleOutput.cs
using System.IO;
using System;

namespace CSharpFilesAndStreams
{
    class ErrOut
    {
        static void Main()
        {
            int a = 10, b = 0;
            int result;
            Console.Out.WriteLine("This will generate an exception.");

            try
            {
                result = a / b; // generate an exception
            }
            catch (DivideByZeroException exc)
            {
                Console.Error.WriteLine(exc.Message);
            }
        }
    }
}

// Output
   This will generate an exception.
   Attempted to divide by zero.

हालांकि Console.Out व Console.Error दोनों समान रूप से Console पर ही Output को Display कर रहे हैं। लेकिन Console.Error की विशेषता ये है कि इसे किसी अन्‍य Device पर Write करने के लिए Redirect किया जा सकता है।

उदाहरण के लिए यदि हम चाहते हैं कि हमारे Program में Generate होने वाली सभी प्रकार की Errors एक Error Log File में Write हों, तो हम Console.Error को किसी File में Data Write करने के लिए Redirect कर सकते हैं। जबकि Console.Out के साथ ऐसा नहीं किया जा सकता।

C# Stream - Classes
C# FileStream

C# in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

C#.NET in Hindi | Page:908 | Format: PDF

BUY NOW GET DEMO REVIEWS