Data Types
Value Types
Boolean Type
The boolean data type is used for representing logical values in programming. In most programming languages, including C#, it is commonly denoted as bool
. The bool
type can have only two possible values: true
or false
.
Use cases for the boolean type include:
- Conditional Statements: Boolean values are frequently used in conditional statements (e.g., if, else, switch) to determine the flow of execution based on whether a condition is true or false.
- Loop Control: Boolean expressions are often used to control the flow of loops (e.g., while, for, do-while). The loop continues as long as the boolean condition remains true.
- Method Returns: Methods can return boolean values to indicate the success or failure of an operation.
- Flagging State: Boolean variables are commonly used as flags to represent the state of a system or a specific feature.
- Logical Operations: Boolean values can be combined using logical operators such as && (AND), || (OR), and ! (NOT) to form more complex conditions.
Character type
In C#, the char type represents a single 16-bit Unicode character. It is used to store individual characters, such as letters, digits, punctuation marks, and special symbols. The char type is part of the integral data types in C#, and it is commonly used to work with single characters in strings or to represent individual characters in various contexts.
Char literals are single characters enclosed in single quotes. Examples:
Escape Sequences
In C#, escape sequences are special sequences of characters that represent certain non-printable or special characters within a string. Escape sequences are used to include characters in strings that are difficult to represent directly. They are identified by a backslash \ followed by a specific character or combination of characters.
Here are some common escape sequences in C#:
Newline (\n)
Represents a newline character. When encountered in a string, it starts a new line.
Carriage Return (\r)
Represents a carriage return character. It is often used in combination with newline to represent a newline on different systems.
Tab (\t)
Represents a horizontal tab character. It is used to create horizontal space in the text.
Backspace (\b)
Represents a backspace character. It moves the cursor one position to the left.
Single Quote (\')
Represents a single quote character.
Double Quote (\")
Represents a double quote character.
Int Type
In C#, the int type is used to represent 32-bit signed integers. It is one of the integral data types in C# and is commonly used to store whole numbers without decimal points. The int type has a range of values from -2,147,483,648 to 2,147,483,647.
Float Type
In C#, the float
type is used to represent single-precision floating-point numbers. It is one of the numeric data types in C# and is commonly used for representing real numbers that require decimal points. The float
type provides a compromise between precision and memory usage, as it uses less memory compared to the double
type but has a smaller range and less precision.
Double Type
In C#, the double
type is used to represent double-precision floating-point numbers. It is part of the numeric data types in C# and provides a higher precision compared to the float
type. The double
type is commonly used for representing real numbers that require decimal points and a larger range of values.
Decimal Type
C# also includes a floating-point type used for currency values. It is more accurate than using the double
type.
Note
A floating-point literal (Ex. 34.23
) is considered a double
type. A float
literal is expressed by succeeding the literal value with an 'F' (Ex. 34.23F
) and a decimal
literal with an 'M' (Ex. 34.23M
).
Reference Types
Object Type
object
is a data type that is the base for all other data types. It is an alias for the System.Object
class in the Common Type System (CTS).
Future Lesson
The object type will be explained more in future modules.
String Type
The string
type, which is the alias to the System.String
.NET type in the Common Type System (CTS), represents a series of characters.
Tip
It is best practice to be familiar with the members of the string
type for any programming language you are developing with.
To understand what you can do with strings in C#, check out the String Class Documentation.
Type Conversions
Type conversion refers to the process of converting a value from one data type to another. Type conversion is necessary when you want to perform operations or assignments involving different data types. There are two main types of type conversion: implicit conversion and explicit conversion.
Implicit Conversion (Widening Conversion)
Implicit conversion, also known as widening conversion, is performed by the compiler automatically when there is no risk of losing data. It occurs when a smaller data type is converted to a larger data type.
In the example above, the int value is implicitly converted to a double value, as there is no loss of precision.
Explicit Conversion (Narrowing Conversion)
Explicit conversion, also known as narrowing conversion, requires a cast operator and is performed when there is a risk of losing data. The programmer explicitly indicates the conversion using a cast.
double doubleValue = 3.14;
// Explicit conversion from double to int
int intValue = (int)doubleValue;
In this example, the double value is explicitly converted to an int value. Note that information might be lost in this conversion, as decimals are truncated.
Common Conversion Methods
Casting
The cast operator (type) is used for explicit conversion.
Convert Class
The Convert class provides methods for converting between types.
ToString Method
The ToString method is used to convert a value to its string representation.
Parse Methods
Parsing methods are used to convert strings to other types.
ToString and Parse in Enumerations
Enumerations can be converted to strings and parsed from strings.
enum Color { Red, Green, Blue }
string colorString = Color.Red.ToString();
Color parsedColor = (Color)Enum.Parse(typeof(Color), "Green");
It's essential to be cautious with explicit conversions, especially when there's a risk of losing data. It's recommended to use conversion methods that handle potential errors gracefully, such as TryParse for parsing and Convert methods for basic conversions.
Other Useful Types
DateTime
The DateTime
type represents dates and times with values ranging from January 1, 0001, 00:00:00 (midnight) to 11:59:59 PM, December 31, 9999 12. It is used to represent an instant of time.
Note
DateTime
is a struct
, not a class. You can think of a struct
like a class, but it is a value type.
You can create a new instance of DateTime
using one of its constructors. For example, you can create a new instance of the DateTime
that represents the current date and time using the following code:
The DateTime
struct provides a variety of methods that allow you to manipulate dates and times. For example, you can use the AddDays
method to add a specified number of days to a date, or the ToString
method to convert a date to a string representation.
Future Lesson
The DateTime
type will be used in a future module.