9.3 Value types


C# value types are close to Java primitives, but there are significant differences. Value types will be your enums (see Chapter 25) and structs (see Chapter 26). Simple types (or primitive types of Java) such as int , byte , and float are special predefined struct types which you can use directly.

While reference types store a reference to an object on the heap, value types directly contain their data. As such, similar to the case of Java's primitives, when you assign one value type to another, a new value is created. Each of the value type variables will store their own copy of the values, and changing the value of one will not affect the value stored in the other. The code below illustrates this:

 1: using System;  2:  3: public class Test{  4:  5:   public static void Main(){  6:     int k = 0;  7:     int j = k;  8:     k = 1;  9:     Console.WriteLine("k is " + k); 10:     Console.WriteLine("j is " + j); 20:   } 21: } 

Output:

 c:\expt>test k is 1 j is 0 

Both value type variables k and j store their own copy of their value due to the assignment on line 7. Assignment of a value type to a variable creates a copy of the value being assigned.

Value types may not be null and operations involving value types are performed on the stack instead of the heap. Value types are 'passed by value' across methods while reference types are 'passed by reference'. [3]

[3] The phrase 'pass by reference' across methods can be controversial and is the source of confusion in several programming books. Since a reference type variable stores the address of an object, when you pass a reference type value over to a method you are actually passing the address of the object over, thus prompting some to argue that a reference type variable is also passed 'by value'. So when Mughal and Rasmussen stated in their book A Programmer's Guide to Java Certification that "in Java, all parameters are passed by value", it is not incorrect.

In the discussion here, I shall focus on the predefined simple types of C#. Looking at the type categorization diagram given in Figure 9.1, you will realize that simple types are actually struct types, which are, in turn , value types. Structs will be introduced in Chapter 26.

C# has many more predefined simple types than Java “ these are shown in Table 9.2.

Table 9.2. Comparing simple types in C# and Java

Simple type category

Java

C#

Size (in bytes)

Comments

Non-numeric

boolean

bool

-

Stores only true or false

Numeric: integral

byte

sbyte

1

Range: -2 7 to -2 7 -1

 

-

byte

1

Range: 0 to +2 8 -1

 

short

short

2

Range: -2 15 to +2 15 -1

 

-

ushort

2

Range: 0 to +2 16 -1

 

int

int

3

Range: -2 31 to +2 31 -1

 

-

uint

3

Range: 0 to +2 32 -1

 

long

long

4

Range: -2 63 to +2 63 -1

 

-

ulong

4

Range: 0 to +2 64 -1

Numeric: floating point

float

float

4

Single precision floating point

double

double

8

Double precision floating point

-

decimal

16

High precision decimal notation with 28 significant digits

(Numeric) character

char

char

2

Single Unicode character

Like Java

  • You can specify that a number be a long by appending L (or l ) after it. You can specify that a floating point number be a float by appending an F (or f ). Table 9.3 shows the special character you should put after a number to specify its type in C#.

    Table 9.3. Special postfix characters on numbers to depict its type. The character to append is not case sensitive “ ul is the same as UL

    Append character

    Type

    Example

    Comments

    L

    long

    long temp = 999L;

    -

    U

    uint

    uint temp = 999U;

    N/A in Java “ Java has no unsigned type

    UL

    ulong

    ulong temp = 999UL

    N/A in Java “ Java has no unsigned type

    F

    float

    float temp = 9.99F

    -

    M

    decimal

    decimal temp = 9.99M

    N/A in Java “ Java has no decimal type

  • If the special character is not specified, by default all floating point numbers are considered to be double values and all integral numbers are considered to be int values.

    In both Java and C#, the statement float f = 9.9; causes a compiler error because 9.9 is defaulted to a double value, and an attempt is made to perform an illegal implicit cast of a double to a float (a narrowing cast like this requires explicit casting).

  • You can use the hexadecimal representation 0xYYYY (where YYYY represents the hexadecimal value) instead of the common base 10 decimal value.

     int temp1 = 65; int temp2 = 0x41; 

    In this case, the expression (temp1==temp2 ) will be true for both Java and C# since 41 16 is equivalent to 65 10 .

  • You can use the Unicode representation \uYYYY (where YYYY represents the Unicode value) for character assignment or in string literals. Like Java, characters in C# are 16-bit Unicode values instead of 8-bit ASCII values.

     char temp4 = '\u0041'; //Unicode char 41(hex) is 'A' System.Console.WriteLine(temp4); // prints out A; 

Unlike Java

  • Note however, that C# does not support Java's octal representation 0YYY (where YYY is the octal value), and instead ignores any preceding zeros on any numbers.

     // in Java int temp3 = 065; // 65 base 8 System.out.println(temp3); // prints out 53 // in C# int temp3 = 065; // treated as 65 base 10 System.Console.WriteLine(temp3);// prints out 65 
  • C# has unsigned integral types while Java does not.

  • While the short , int , and long integral types of Java and C# are identical in range, the Java byte is signed but the C# byte is unsigned. C#'s signed 8-bit integral type is sbyte .



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net