Tài liệu Working with Structure Types doc - Pdf 87



Working with Structure Types
You saw in Chapter 8 that classes define reference types that are always created on the
heap. In some cases, the class can contain so little data that the overhead of managing the
heap becomes disproportionate. In these cases it is better to define the type as a structure.
Because structures are stored on the stack, the memory management overhead is often
reduced (as long as the structure is reasonably small).
A structure can have its own fields, methods, and constructors just like a class (and unlike
an enumeration), but it is a value type and not a reference type.
Common Structure Types
You may not have realized it, but you have already used structures in previous exercises
in this book. In C#, the primitive numeric types int, long, and float, are aliases for the
structures System.Int32, System.Int64, and System.Single, respectively. This means that
you can actually call methods on variables and literals of these types. For example, all of
these structures provide a ToString method that can convert a numeric value to its string
representation. The following statements are all legal statements in C#:
int i = 99;
Console.WriteLine(i.ToString());
Console.WriteLine(55.ToString());
float f = 98.765F;
Console.WriteLine(f.ToString());
Console.WriteLine(98.765F.ToString());
You don't see this use of the ToString method very often, because the Console.WriteLine
method calls it automatically when it is needed. Use of the static methods exposed by
these structures is much more common. For example, in earlier chapters the static
Int32.Parse method was used to convert a string to its corresponding integer value:
string s = "42";
int i = Int32.Parse(s);
NOTE
Because int is simply an alias for Int32, you can also use int.Parse.

there is no way to ensure that public fields contain valid values. For example, anyone
could set the value of minutes or seconds to a value greater than 60. A better idea is to
make the fields private and provide your structure with constructors and methods, as
shown in this example:
struct Time
{
public Time(int hh, int mm, int ss)
{
hours = hh % 24;
minutes = mm % 60;
seconds = ss % 60;
}

public int Hours()
{
return hours;
}
...
private int hours, minutes, seconds;
}
NOTE
By default, you cannot use many of the common operators on your own structure types.
For example, you cannot use operators such as == and != on your own struct type
variables. However, you can explicitly declare and implement operators for your own
struct types. The syntax for doing this is covered in Chapter 19.
Use structs to implement simple concepts whose main feature is their value. For example,
an int is a value type because its main feature is its value. If you have two int variables
that contain the same value (such as 42), one is as good as the other. When you copy a
value type variable, you get two copies of the value. In contrast, when you copy a
reference type variable, you get two references to the same object. In summary, use

a class, because Time is a struct, it fails to compile:
struct Time
{
public Time(int hh, int mm)
{
hours = hh;
minutes = mm;
} // compile time error: seconds not initialized
...
private int hours, minutes, seconds;
}

In a class, you can initialize instance fields at their point of declaration. In a struct,
you cannot. For instance, the following example would compile if Time were a
class, but because Time is a struct, it causes a compile-time error (reinforcing the
rule that every structure must initialize all its fields in all its constructors):

struct Time

{

...

private int hours = 0; // compile time error

private int minutes;

private int seconds;
}
The following table summarizes the main differences between a structure and a class.

declare structures, the next step is to use them to create values.
Declaring Structure Variables
After you have defined a struct type, you can use it in exactly the same way as any other
type. For example, if you have defined the Time struct, you can create variables, fields,
and parameters of type Time, as shown in this example:
struct Time
{
...
private int hours, minutes, seconds;
}

class Example
{
public void Method(Time parameter)
{
Time localVariable;
...
}

private Time currentTime;
}
Understanding Structure Initialization


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status