Tải bản đầy đủ - 0 (trang)
2-15. Read User Input from the Console

2-15. Read User Input from the Console

Tải bản đầy đủ - 0trang

CHAPTER 2 ■ DATA MANIPULATION



The returned ConsoleKeyInfo object contains details about the key pressed. The details are

accessible through the properties of the ConsoleKeyInfo class (summarized in Table 2-5).

Table 2-5. Properties of the ConsoleKeyInfo Class



Property



Description



Key



Gets a value of the System.ConsoleKey enumeration representing the key pressed. The

ConsoleKey enumeration contains values that represent all of the keys usually found on a

keyboard. These include all the character and function keys; navigation and editing keys

like Home, Insert, and Delete; and more modern specialized keys like the Windows key,

media player control keys, browser activation keys, and browser navigation keys.



KeyChar



Gets a char value containing the Unicode character representation of the key pressed.



Modifiers



Gets a bitwise combination of values from the System.ConsoleModifiers enumeration that

identifies one or more modifier keys pressed simultaneously with the console key. The

members of the ConsoleModifiers enumeration are Alt, Control, and Shift.



The KeyAvailable method of the Console class returns a bool value indicating whether input is

available in the input buffer without blocking your code.



The Code

The following example reads input from the console one character at a time using the ReadKey method. If

the user presses F1, the program toggles in and out of “secret” mode, where input is masked by asterisks.

When the user presses Esc, the console is cleared and the input the user has entered is displayed. If the

user presses Alt+X or Alt+x, the example terminates.

using System;

using System.Collections.Generic;

namespace Apress.VisualCSharpRecipes.Chapter02

{

class Recipe02_15

{

public static void Main()

{

// Local variable to hold the key entered by the user.

ConsoleKeyInfo key;

// Control whether character or asterisk is displayed.

bool secret = false;

// Character List for the user data entered.

List input = new List();



94



www.it-ebooks.info



CHAPTER 2 ■ DATA MANIPULATION



string msg = "Enter characters and press Escape to see input." +

"\nPress F1 to enter/exit Secret mode and Alt-X to exit.";

Console.WriteLine(msg);

// Process input until the user enters "Alt+X" or "Alt+x".

do

{

// Read a key from the console. Intercept the key so that it is not

// displayed to the console. What is displayed is determined later

// depending on whether the program is in secret mode.

key = Console.ReadKey(true);

// Switch secret mode on and off.

if (key.Key == ConsoleKey.F1)

{

if (secret)

{

// Switch secret mode off.

secret = false;

}

else

{

// Switch secret mode on.

secret = true;

}

}

// Handle Backspace.

if (key.Key == ConsoleKey.Backspace)

{

if (input.Count > 0)

{

// Backspace pressed, remove the last character.

input.RemoveAt(input.Count - 1);

Console.Write(key.KeyChar);

Console.Write(" ");

Console.Write(key.KeyChar);

}

}

// Handle Escape.

else if (key.Key == ConsoleKey.Escape)

{

Console.Clear();

Console.WriteLine("Input: {0}\n\n",

new String(input.ToArray()));

Console.WriteLine(msg);

input.Clear();

}

// Handle character input.



95



www.it-ebooks.info



CHAPTER 2 ■ DATA MANIPULATION



else if (key.Key >= ConsoleKey.A && key.Key <= ConsoleKey.Z)

{

input.Add(key.KeyChar);

if (secret)

{

Console.Write("*");

}

else

{

Console.Write(key.KeyChar);

}

}

} while (key.Key != ConsoleKey.X

|| key.Modifiers != ConsoleModifiers.Alt);

// Wait to continue.

Console.WriteLine("\n\nMain method complete. Press Enter");

Console.ReadLine();

}

}

}



2-16. Using Large Integer Values

Problem

You need to work with an integer value that exceeds the size of the default numeric types.



Solution

Use the System.Numerics.BigInteger class.



How It Works

Numeric values in the .NET Framework have maximum and minimum values based on how much

memory is allocated by the data type. The System.Numerics.BigInteger class has no such limits, and can

be used to perform operations on very large integer values.

Instances of BigInteger are immutable, and you perform operations using the static methods of the

BigInteger class, each of which will return a new instance of BigInteger as the result—see the code in

this recipe for an example.



The Code

The following example creates a BigInteger with a value that is twice the maximum value of Int64 and

then adds another Int64.MaxValue.



96



www.it-ebooks.info



CHAPTER 2 ■ DATA MANIPULATION



using

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Numerics;



namespace Apress.VisualCSharpRecipes.Chapter02

{

class Recipe2_16

{

static void Main(string[] args)

{

// Create a new big integer.

BigInteger myBigInt = BigInteger.Multiply(Int64.MaxValue, 2);

// Add another value.

myBigInt = BigInteger.Add(myBigInt, Int64.MaxValue);

// Print out the value.

Console.WriteLine("Big Integer Value: {0}", myBigInt);

// Wait to continue.

Console.WriteLine("\n\nMain method complete. Press Enter");

Console.ReadLine();

}

}

}



2-17. Select Collection or Array Elements

Problem

You need to select elements from a collection or an array.



Solution

Use the basic features of Language-Integrated Query (LINQ).



How It Works

LINQ allows you to select elements from a collection based on characteristics of the elements contained

within the collection. The basic sequence for querying a collection or array using LINQ is as follows:



97



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

2-15. Read User Input from the Console

Tải bản đầy đủ ngay(0 tr)

×