Data Types

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/81

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

82 Terms

1
New cards

Identifier

an __________ is a name of a program component programmers use to uniquely identify namespaces, classes, methods, variables, constants, etc.

2
New cards

Identifiers

are user-defined words. For example, in the program shown in Code Listing 1, the __________ are

3
New cards

ConsoleApp, ComputeRectangleArea, length, width, area, WriteLine, and ReadKey.

4
New cards

The following are the syntax rules when naming an identifier in C#:

5
New cards

It must start with a letter of the English alphabet or an underscore character.

6
New cards

The identifier's name can only have any combination of letters, digits, and underscores. White spaces are not allowed.

7
New cards

Identifiers are case sensitive. For example, the identifier Area is not the same with identifiers area or AREA.

8
New cards

It cannot be a reserved keyword.

9
New cards

The classes and methods in C# must always begin with a capital letter.

10
New cards
<p>Valid Identifiers</p>

Valid Identifiers

11
New cards
<p>Invalid Identifiers</p>

Invalid Identifiers

12
New cards

number

1score is invalid because it begins with ______.

13
New cards

white space

first Name is invalid because it contains __________.

14
New cards

reserved keyword

class is an invalid identifier because it is a _______________.

15
New cards

Keywords

are reserved words a programming language uses for its own use, and they have a special predefined meaning to the compiler.

16
New cards

Keywords

These cannot be used as identifiers. If you do use a _______in a program, the compiler will throw an error message.

17
New cards
<p>Table 3 shows the list of keywords in C#.</p>

Table 3 shows the list of keywords in C#.

18
New cards

Variable

is an identifier and a memory location that stores a specific value.

19
New cards

Variable

holdd a value that can be changed during program execution.

20
New cards

For example, a variable named score assigned an initial value of 25. When the program starts, the value of variable score will change to 85.

21
New cards

data_type identifier;

The basic syntax of declaring a variable is _____________

22
New cards

identifier

is the name of the variable. For example, int score;

23
New cards

Variables

are initialized, or assigned a value, with an equal sign followed by the value. The following are some valid examples of declaring and initializing variables:

24
New cards

You can initialize a variable at the time of declaration. For example: int score = 25;

25
New cards

You can declare and initialize more than one (1) variable of the same time data type using a comma in a single statement: int score, age; score = 85; age = 24; int length = 8, width = 5;

26
New cards

When creating a program, you must define a variable with a meaningful name that is easy to understand what value must store on it. For example, the variables gameScore and age must store integer type values.

27
New cards

camelCase

Use _________notation that starts with

28
New cards

a lowercase letter for naming local variables. For example, numberOfStudents, age, totalPrice, etc.

29
New cards

Constant

is an identifier and a memory location whose value cannot be changed during program execution.

30
New cards

Constants

must initialize a value at the time of declaration.

31
New cards

const data_type IDENTIFIER = value;

The basic syntax of initializing a constant in C# is as follows: _____________________

32
New cards

const

Constants in C# are defined using the _____ keyword. For example: const double PI = 3.14159;.

33
New cards

In the example, the constant PI with the value of 3.14159 cannot be changed during program execution. The name of the constants must be in all capital to make it easy to identify that its value must not change.

34
New cards

Data types

are used to specify a set of values and their operations associated with variables or constants.

35
New cards

Data types

The values have particular __________ because they are stored in memory in the same format and have the same operations defined for them.

36
New cards

Variable

A ________ or constant stores data of a specific type.

37
New cards

When declaring a variable or constant to store a data, the appropriate data type for that data must be identified.

38
New cards

The data type will instruct the compiler what kind of value a variable or constant can hold and its operations.

39
New cards

There are two (2) types of primitive data types in C#:

40
New cards

Value types

These data types store the value directly. The data type int is a value type that stores its value in a memory location directly.

41
New cards

Reference types

These data types do not store the actual value in a variable or constant. Instead, they store the reference (or address) where the value is stored.

42
New cards

The class objects, strings, and arrays are reference types because they hold references to blocks of memory and are managed on the heap.

43
New cards

Value type

A data type is a ___________ if it holds the data within its own memory allocation.

44
New cards

Value types

directly store the values within the variable.

45
New cards

For example, consider the following figure:

46
New cards
<p>Figure 1. Memory allocation for value type</p>

Figure 1. Memory allocation for value type

47
New cards

In Figure 1, the variable length of int type is assigned a value of 50. When this statement is executed, the compiler will instruct the computer system to directly store 50 in the memory space allocated for the variable length.

48
New cards
<p>Table 4. Value types in C#</p>

Table 4. Value types in C#

49
New cards

Reference type

does not store an actual value, but it stores the address where the value is stored.

50
New cards

Reference types

contain the memory locations of where the value is stored.

51
New cards
<p>Figure 2. Memory allocation for reference type</p>

Figure 2. Memory allocation for reference type

52
New cards

In Figure 2, the compiler will instruct the computer system to select a different memory location for the variable strName. The content of the variable strName is "0x7600", which is the address or memory location of the actual string value "Jess Diaz".

53
New cards

Strings, Objects, Arrays, and Delegates

What are the reference types in C#?

54
New cards

Type conversion / type casting

is the process of converting a value of one (1) type of data to another data type.

55
New cards

Type conversion / type casting

This is used to ensure that a function correctly processes the variables.

56
New cards

Type conversion

Converting a string to an integer is an example of _______________.

57
New cards

The following are the two (2) forms of type casting in C#:

58
New cards

Implicit conversion

This is the conversion of a lower precision data type to a value of higher precision data type.

59
New cards

A compiler automatically performs implicit conversion if the precision of data type to convert is lower than precision of preferred data type.

60
New cards

For example, a variable of short data type is capable of storing values up to 32,767, and the maximum value allowed into a byte is 255; so, there will be no problem when you convert the value of byte type into a value of short data type.

61
New cards

An example of implicit conversion includes converting the value of int data type to a value of double data type, because int data type has a lower precision than double data type.

62
New cards
<p>Table 5. Implicit numeric conversions</p>

Table 5. Implicit numeric conversions

63
New cards
<p>Table 5. Implicit numeric conversions in C#.</p>

Table 5. Implicit numeric conversions in C#.

64
New cards

Note: There is no implicit conversion of any data type to char type, so the values of the other integral types do not automatically convert to the char type.

65
New cards

The bool and double data types have no possible implicit conversions to the other data types.

66
New cards

Explicit conversion

Converting a higher precision data type to a lower precision data type is invalid, and the compiler will return an error.

67
New cards

For example, the statements

double num1 = 25.0;

int num2 = a;

will return an error because the precision of variable num1 of double data type is higher than the variable num2 of int type.

68
New cards

Cast operator

However, explicit conversions allow the users to convert a value of higher precision data type into a value of lower precision data type by using a ______________.

69
New cards

(data_type) data_to_convert;

The following is the general syntax of performing explicit conversion in C#:

70
New cards

(data_type)

is the cast operator that will create a converted copy of the value in data_to_convert. For example, the following statements uses a cast operator to explicitly convert data types: //first example int num = 25; byte b = (byte) num; /

71
New cards

//first example

int num = 25;

byte b = (byte) num;

72
New cards

//second example double price = 75.5; int varA = (int) price; //the value of variable a will be 75

73
New cards

In the first example, the value 25 is assigned to the variable num of int type, then the converted copy of value from variable num is assigned to the variable b. The value stored in variable num is still an integer type because the cast operator created a converted copy of its value as byte data type.

74
New cards

Explicit conversion involves the risk of losing information, i.e., from double to int data type.

For example, in the second example, the value of the variable price of double data type is copied and converted into int data type then stored in the variable varA of int data type. The cast operator dropped the decimal part of the floating-point number 75.5 to convert it to 75 as int data type.

75
New cards

The following statements shows some of the ways on how to use the cast operator:

76
New cards

int a = (int) 7.9;

//the return value of a is 7

77
New cards

double b = (double) (5 + 3);

//the value of b is 8.0

78
New cards

Console.WriteLine((int) 2.5);

//the output is 2

79
New cards

int c = 64; char d = (char) c;

//the return value of d is the character '@'

80
New cards
<p>Table 6. Explicit conversions </p>

Table 6. Explicit conversions

81
New cards
<p>Table 6. Explicit conversions </p>

Table 6. Explicit conversions

82
New cards

The explicit conversion of a data type to char data type will return the corresponding character of the value from the ASCII table.

int a = 64;

char b = (char) a;

//the value of b is the character '@' because it is the corresponding character of decimal value 64 in ASCII table