Chapter 2: Data Types

1. What is the difference between signed and an unsigned integers?

signed integers have the type alias int and BCL name Systm.Int32. Their range is

–2,147,483,648 to 2,147,483,647.

unsigned integers have the type alias uint and BCL name Systm.Uint32. Their range is

0 to 4,294,967,295

2. When should floating-point numbers be used, and when should decimal numbers be used?

Floating Point numbers should be used to represent larger numbers outside the range of decimal numbers, and/or if speed is a larger concern then precision, because operations performed on floating-point numbers tend to be faster. .

Decimal numbers are better for situations that require precise precision, such as financial calculations.

3. How is hexadecimal notation used? Can you give an example of such notation?

Hexadecimal notation represents numbers in base 16 notation, instead of decimal notation, meaning sixteen symbols are used to represent the numbers 0 to 15 (0-9, A-F). For example, the hexadecimal number 2AF7 is equal, in decimal, to (2 × 163) + (10 × 162) + (15 × 16) + 7, or 10,999.

byte aHexNumber = 0xFF; //represents the decimal number 255

short anotherHexNumber = 0x7FFF; //represents the decimal number 32,767

int yetAnotherHexNumber = 0x7FFFFFFF; //represents the decimal number 2,147,483,647

long oneMoreHexNumber = 0x7FFFFFFFFFFFFFFF; //represents the decimal number 9,223,372,036,854,775,807

//continue to use these numbers just as you would any other decimal type type variable

4. What is the purpose of the StringBuilder class (System.Text.StringBuilder)?

Strings are immutable, meaning that a string value cannot be modified without first creating a new copy of the original string value. The System.Text.StringBuilder class can be used, instead, to modify a string without creating a new copy for each modification. StringBuilder methods are comparable to String class methods.

5. What is the difference between a value type and a reference type?

All types fall into two categories: value types and reference types. Value types contain their values directly; i.e., the variable refers to the same location in memory where the value is stored. Reference types do not contain their values but instead a reference (pointer) to their values stored elsewhere (on the heap).

6. What is the rank of an array?

The rank of the array is the number of dimensions. For example, the following array has rank 2:

int[,] arr = new int[255,255];

7. Write the following code:

  • Create and fill an array with 1000 random integers.
  • Sort the array.
  • Reverse the array.
  • Apply the Resize method to resize the array to twice its original size.
  • Copy the array to another array.

staticvoidMain(string[] args)

{

int[] intArray = newint[1000];

Random rnd = newRandom();

// Fill the Array

for (int i = 0; i < intArray.Length; i++)

{

intArray[i] = rnd.Next();

}

// Sort the Array

System.Array.Sort(intArray);

// Reverse the Array

System.Array.Reverse(intArray);

// Resize the Array (double it)

System.Array.Resize<int>(ref intArray, intArray.Length * 2);

}

Note: LINQ (Chapters 14&15) provides additional operators that would be preferable

Supplemental Exercises for Essential C# 4.0 by Mark Michaelis (ISBN 0-321-69469-4). Page 1