PART II
C h a p t e r 2 1 :
E x p l o r i n g t h e S y s t e m N a m e s p a c e
653
else
Console.WriteLine("nums contains no negative values.");
}
}
The output is shown here:
Contents of nums: 1 4 -1 5 -9
nums contains a negative value.
First negative value is : -1
In the program, the method passed to
Exists( )
and
Find( )
for the predicate is
IsNeg( )
.
Notice that
IsNeg( )
is declared like this:
static bool IsNeg(int v) {
The methods
Exists( )
and
Find( )
will automatically pass the elements of the array (in
sequence) to
v
. Thus, each time
IsNeg( )
is called,
v
will contain the next element in the array.
Using an Action
The
Action
delegate is used by
Array.ForEach( )
to perform an action on each element of an
array. There are various forms of
Action
, each taking a different number of type parameters.
The one used here is
public delegate void Action (T
obj
)
The object to be acted upon is passed in
obj
. When used with
ForEach( )
, each element of the
array is passed to
obj
in turn. Thus, through the use of
ForEach( )
and
Action
, you can, in a
single statement, perform an operation over an entire array.
The following program demonstrates both
ForEach( )
and
Action
. It first creates an
array of
MyClass
objects, then uses the method
Show( )
to display the values. Next, it uses
Neg( )
to negate the values. Finally, it uses
Show( )
again to display the negated values.
These operations all occur through calls to
ForEach( )
.
// Demonstrate an Action.
using System;
class MyClass {
public int i;
public MyClass(int x) { i = x; }
}
class ActionDemo {
// An Action method.
// It displays the value it is passed.
static void Show(MyClass o) {
Console.Write(o.i + " ");
}
// Another Action method.
// It negates the value it is passed.
www.freepdf-books.com
654
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
static void Neg(MyClass o) {
o.i = -o.i;
}
static void Main() {
MyClass[] nums = new MyClass[5];
nums[0] = new MyClass(5);
nums[1] = new MyClass(2);
nums[2] = new MyClass(3);
nums[3] = new MyClass(4);
nums[4] = new MyClass(1);
Console.Write("Contents of nums: ");
// Use action to show the values.
Array.ForEach(nums, ActionDemo.Show);
Console.WriteLine();
// Use action to negate the values.
Array.ForEach(nums, ActionDemo.Neg);
Console.Write("Contents of nums negated: ");
// Use action to show the values again.
Array.ForEach(nums, ActionDemo.Show);
Console.WriteLine();
}
}
The output is shown here:
Contents of nums: 5 2 3 4 1
Contents of nums negated: -5 -2 -3 -4 -1
BitConverter
In programming one often needs to convert a built-in data type into an array of bytes. For
example, some hardware device might require an integer value, but that value must be sent
one byte at a time. The reverse situation also frequently occurs. Sometimes data will be
received as an ordered sequence of bytes that needs to be converted into one of the built-in
types. For example, a device might output integers, sent as a stream of bytes. Whatever
your conversion needs, .NET provides the
BitConverter
class to meet them.
BitConverter
is
static
class. It contains the methods shown in Table 21-13. It defines the
following field:
public static readonly bool IsLittleEndian
This field is
true
if the current environment stores a word with the least significant byte first
and the most significant byte last. This is called “little-endian” format.
IsLittleEndian
is
false
if the current environment stores a word with the most significant byte first and the
least significant byte last. This is called “big-endian” format. Intel Pentium–based machines
use little-endian format.
www.freepdf-books.com
Do'stlaringiz bilan baham: |