C# – Data Types

Constant or variable data type

A data type describes the format and size of (amount of memory occupied by) a data item and defines what types of operations can be performed with the item. C# provides for 15 basic, or intrinsic types, of data, as shown in Table 2-1.

15 basic types – most common int, double, decimal, char, string, bool

In C#, nine data types are considered integral data types—that is, types that store whole numbers. The nine types are byte, sbyte, short, ushort, int, uint, long, ulong, and char. The first eight always represent whole numbers, and the ninth type, char, is used for characters such as A or a.

Saving memory is seldom an issue for an application that runs on a PC. However, when you write applications for small devices with limited memory, such as smartphones, conserving memory becomes more important.

Variable Alignment

When you use a series of WriteLine() statements to display a list of variable values, the values are not automatically right-aligned as you normally expect numbers to be.

If you use a second number within the curly braces in a format string, you can specify alignment and field size. By default, numbers are right-aligned in their fields. If you use a negative value for the field size in a Write() or WriteLine() statement, the displayed value will be left-aligned in the field.

Float Point Data Types

A float data type can hold as many as seven significant digits of accuracy. For example, a float assigned the value 1234.56789 will appear as 1234.568 because it is accurate only to the seventh digit (the third digit to the right of the decimal point).

A double data type can hold 15 or 16 significant digits of accuracy. For example, a double given the value 123456789.987654321 will appear as 123456789.987654 because it is accurate to only the fifteenth digit (the sixth digit to the right of the decimal point).

The decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. A decimal is significant to 28 or 29 digits.

Formating Strings

The format specifier can be one of nine built-in format characters that define the most commonly used numeric format types. The precision specifier controls the number of significant digits or zeros to the right of the decimal point.

Arithmetic Operators

Five most commonly used arithmetic operators.

+  –  *  /  %

You use these operators to manipulate values in your programs. The values that operators use in expressions are called operands. These arithmetic operators are called binary operators because you use two operands with each—one value to the left of the operator and another value to the right of it.

When you combine mathematical operations in a single statement, you must understand operator precedence, or the rules that determine the order in which parts of a mathematical expression are evaluated.

Parentheses. Evaluate expressions within parentheses first. Multiplication, division, remainder. Evaluate these operations next from left to right. Addition, subtraction. Evaluate these operations next from left to right.

Shortcut Operators

+= add and assign

-= subtract

*= multiply

/= divide

++ increase by 1

— decrease by 1

bool Data Type

Boolean logic is based on true-or-false comparisons. An int variable can hold millions of different values at different times, but a Boolean variable can hold only one of two values— true or false. You declare a Boolean variable by using the data type bool.

bool doesEmployeeReceiveOvertime = hoursWorked > 40;

bool isHighTaxBracket = annualIncome > 100000;