CATEGORII DOCUMENTE |
C# supports two kinds of types: value types and reference types. Value types include simple types (e.g., char, int, and float), enum types, and struct types. Reference types include class types, interface types, delegate types, and array types.
Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types store references to objects. With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
The example
using System;
class
Class1
class
Test
,
', val1, val2);
Console.WriteLine('Refs:
, ', ref1.Value, ref2.Value);
}
}
shows this difference. The output of the program is
Values: 0, 123
Refs: 123, 123
The assignment to the local variable val1 does not impact the local variable val2 because both local variables are of a value type (the type int) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123; affects the object that both ref1 and ref2 reference.
The lines
Console.WriteLine('Values:
, ', val1, val2);
Console.WriteLine('Refs: , ', ref1.Value, ref2.Value);
deserve further comment, as they demonstrate some of the string formatting behavior of Console.WriteLine, which takes a variable number of arguments. The first argument is a string, which may contain numbered placeholders like and . Each placeholder refers to a trailing argument with referring to the second argument, referring to the third argument, and so on. Before the output is sent to the console, each placeholder is replaced with the formatted value of its corresponding argument.
Developers can define new value types through enum and struct declarations, and can define new reference types via class, interface, and delegate declarations. The example
using System;
public
enum Color
public
struct Point
public
interface IBase
public
interface IDerived: IBase
public
class A
}
public
class B: A, IDerived
public void G()
override protected void H()
}
public delegate void EmptyDelegate();
shows an example or two for each kind of type declaration. Later sections describe type declarations in detail.
C# provides a set of predefined types, most of which will be familiar to C and C++ developers.
The predefined reference types are object and string. The type object is the ultimate base type of all other types. The type string is used to represent Unicode string values. Values of type string are immutable.
The predefined value types include signed and unsigned integral types, floating point types, and the types bool, char, and decimal. The signed integral types are sbyte, short, int, and long; the unsigned integral types are byte, ushort, uint, and ulong; and the floating point types are float and double.
The bool type is used to represent boolean values: values that are either true or false. The inclusion of bool makes it easier to write self-documenting code, and also helps eliminate the all-too-common C++ coding error in which a developer mistakenly uses "=" when "==" should have been used. In C#, the example
int i =
;
F(i);
if (i = 0) // Bug: the test should be (i
== 0)
G();
is invalid because the expression i = 0 is of type int, and if statements require an expression of type bool.
The char type is used to represent Unicode characters. A variable of type char represents a single 16-bit Unicode character.
The decimal type is appropriate for calculations in which rounding errors caused by floating point representations are unacceptable. Common examples include financial calculations such as tax computations and currency conversions. The decimal type provides 28 significant digits.
The table below lists the predefined types, and shows how to write literal values for each of them.
Type |
Description |
Example |
object |
The ultimate base type of all other types |
object o = null; |
string |
String type; a string is a sequence of Unicode characters |
string s = 'hello'; |
sbyte |
8-bit signed integral type |
sbyte val = 12; |
short |
16-bit signed integral type |
short val = 12; |
int |
32-bit signed integral type |
int val = 12; |
long |
64-bit signed integral type |
long
val1 = 12; |
byte |
8-bit unsigned integral type |
byte
val1 = 12; |
ushort |
16-bit unsigned integral type |
ushort
val1 = 12; |
uint |
32-bit unsigned integral type |
uint
val1 = 12; |
ulong |
64-bit unsigned integral type |
ulong
val1 = 12; |
float |
Single-precision floating point type |
float val = 1.23F; |
double |
Double-precision floating point type |
double
val1 = 1.23; |
bool |
Boolean type; a bool value is either true or false |
bool
val1 = true; |
char |
Character type; a char value is a Unicode character |
char val = 'h'; |
decimal |
Precise decimal type with 28 significant digits |
decimal val = 1.23M; |
Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32. As a matter of style, use of the keyword is favored over use of the complete system type name.
Predefined value types such as int are treated specially in a few ways but are for the most part treated exactly like other structs. Operator overloading enables developers to define new struct types that behave much like the predefined value types. For instance, a Digit struct can support the same mathematical operations as the predefined integral types, and can define conversions between Digit and predefined types.
The predefined types employ operator overloading themselves. For example, the comparison operators == and != have different semantics for different predefined types:
Two expressions of type int are considered equal if they represent the same integer value.
Two expressions of type object are considered equal if both refer to the same object, or if both are null.
Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null.
The example
class
Test
}
produces the output
True
False
because the first comparison compares two expressions of type string, and the second comparison compares two expressions of type object.
The predefined types also have predefined conversions. For instance, conversions exist between the predefined types int and long. C# differentiates between two kinds of conversions: implicit conversions and explicit conversions. Implicit conversions are supplied for conversions that can safely be performed without careful scrutiny. For instance, the conversion from int to long is an implicit conversion. This conversion always succeeds, and never results in a loss of information. Implicit conversions can be performed implicitly, as shown in the example
using System;
class Test
{
static void
int intValue = 123;
long longValue = intValue;
Console.WriteLine(',
', intValue, longValue);
}
}
which implicitly converts an int to a long.
In contrast, explicit conversions are performed with a cast expression. The example
using System;
class Test
{
static void
long longValue =
Int64.MaxValue;
int intValue = (int) longValue;
Console.WriteLine('(int)
= ', longValue, intValue);
}
}
uses an explicit conversion to convert a long to an int. The output is:
(int) 9223372036854775807 = -1
because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions.
Arrays may be single-dimensional or multi-dimensional. Both "rectangular" and "jagged" arrays are supported.
Single-dimensional arrays are the most common type, so this is a good starting point. The example
using System;
class
Test
{
static void
int[] arr = new int[5];
for (int i = 0; i < arr.Length;
i++)
arr[i] = i * i;
for (int i = 0; i < arr.Length;
i++)
Console.WriteLine('arr[]
= ', i, arr[i]);
}
}
creates a single-dimensional array of int values, initializes the array elements, and then prints each of them out. The program output is:
arr[0] =
0
arr[1] = 1
arr[2] = 4
arr[3] = 9
arr[4] = 16
The type int[] used in the previous example is an array type. Array types are written using a non-array-type followed by one or more rank specifiers. The example
class
Test
}
shows a variety of local variable declarations that use array types with int as the element type.
Array types are reference types, and so the declaration of an array variable merely sets aside space for the reference to the array. Array instances are actually created via array initializers and array creation expressions. The example
class
Test
;
int[,] a2 = new int[,] , };
int[,,] a3 = new int[10, 20,
30];
int[][] j2 = new int[3][];
j2[0] = new int[] ;
j2[1] = new int[] ;
j2[2] = new int[] ;
}
}
shows a variety of array creation expressions. The variables a1, a2 and a3 denote rectangular arrays, and the variable j2 denotes a jagged array. It should be no surprise that these terms are based on the shapes of the arrays. Rectangular arrays always have a rectangular shape. Given the length of each dimension of the array, its rectangular shape is clear. For example, the lengths of a3's three dimensions are 10, 20, and 30 respectively, and it is easy to see that this array contains 10*20*30 elements.
In contrast, the variable j2 denotes a "jagged" array, or an "array of arrays". Specifically, j2 denotes an array of an array of int, or a single-dimensional array of type int[]. Each of these int[] variables can be initialized individually, and this allows the array to take on a jagged shape. The example gives each of the int[] arrays a different length. Specifically, the length of j2[0] is 3, the length of j2[1] is 6, and the length of j2[2] is 9.
The element type and shape of an array-including whether it is jagged or rectangular, and the number of dimensions it has-are part of its type. On the other hand, the size of the array-as represented by the length of each of its dimensions-is not part of an array's type. This split is made clear in the language syntax, as the length of each dimension is specified in the array creation expression rather than in the array type. For instance the declaration
int[,,] a3 = new int[10, 20, 30];
has an array type of int[,,] and an array creation expression of new int[10, 20, 30].
For local variable and field declarations, a shorthand form is permitted so that it is not necessary to re-state the array type. For instance, the example
int[] a1 = new int[] ;
can be shortened to
int[] a1 = ;
without any change in program semantics.
The context in which an array initializer such as is used determines the type of the array being initialized. The example
class
Test
{
static void
short[] a = ;
int[] b = ;
long[] c = ;
}
}
shows that the same array initializer syntax can be used for several different array types. Because context is required to determine the type of an array initializer, it is not possible to use an array initializer in an expression context without explicitly stating the type of the array.
C# provides a "unified type system". All types-including value types-derive from the type object. It is possible to call object methods on any value, even values of "primitive" types such as int. The example
using System;
class
Test
}
calls the object-defined ToString method on an integer literal.
The example
class
Test
}
is more interesting. An int value can be converted to object and back again to int. This example shows both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.
This type system unification provides value types with the benefits of object-ness without introducing unnecessary overhead. For programs that don't need int values to act like objects, int values are simply 32-bit values. For programs that need int values to behave like objects, this capability is available on demand. This ability to treat value types as objects bridges the gap between value types and reference types that exists in most languages. For example, a Stack class can provide Push and Pop methods that take and return object values.
public
class Stack
public void Push(object o)
}
Because C# has a unified type system, the Stack class can be used with elements of any type, including value types like int.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 900
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved