0% found this document useful (0 votes)
2 views

Variable_Keyword_DataType_Array

The document provides an overview of variables in C#, explaining their role in storing data, the distinction between value types and reference types, and the concepts of boxing and unboxing. It also covers constants, arrays, and their types, including rectangular and jagged arrays, emphasizing their structure and memory management. Key points include the necessity of initializing variables and constants, and the use of index numbers for accessing array elements.

Uploaded by

trangtthe161410
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Variable_Keyword_DataType_Array

The document provides an overview of variables in C#, explaining their role in storing data, the distinction between value types and reference types, and the concepts of boxing and unboxing. It also covers constants, arrays, and their types, including rectangular and jagged arrays, emphasizing their structure and memory management. Key points include the necessity of initializing variables and constants, and the use of index numbers for accessing array elements.

Uploaded by

trangtthe161410
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Variable_Keywords_DataTypes_Ar

ray
Variable
 A variable is used to store data in a program and is declared with an associated data type.
 A variable has a name and may contain a value.
 A data type defines the type of data that can be stored in a variable.
 A variable is an entity whose value can keep changing during the course of a program.
o Example: The age of a student, the address of a faculty member, and the salary of an
employee are all examples of entities that can be represented by variables.
 In C#, a variable is a location in the computer’s memory that is identified by a unique name
and is used to store a value. The name of the variable is used to access and read the value
stored in it.
 A variable is an entity whose value can keep changing during the course of a program.
 Example: The age of a student, the address of a faculty member, and the salary of an
employee are all examples of entities that can be represented by variables.
 In C#, a variable is a location in the computer’s memory that is identified by a unique name
and is used to store a value. The name of the variable is used to access and read the value
stored in it.

Kiểu tham trị (value type) và kiểu tham chiếu (reference type)
Null pointer
Reference types have null value by default, when they are not initialized.

 For example, a string variable (or any other variable of reference type datatype) without a
value assigned to it.
 In this case, it has a null value, meaning it doesn't point to any other memory location,
because it has no value yet.

A value type variable cannot be null because it holds a value not a memory address

 Value type variables must be assigned some value before use


 The compiler will give an error if you try to use a local value type variable without assigning a
value to it
Stack and heap
Ở đây, ngoặc đóng cuối, thì các giá trị cls1 đẩy ra trước -> I = 4 đẩy cuối cùng.

Nếu ta có câu hỏi rằng nhiều reference type thì đầy heap, thì C# có garbage collector sẽ tự động xóa
chúng đi sau một khoảng thời gian.

Boxing and Unboxing


Boxing is the process of converting a value type to the type object or to any interface type
implemented by this value type.

 When the common language runtime (CLR) boxes a value type, it wraps the value inside a
System.Object instance and stores it on the managed heap.

Unboxing extracts the value type from the object.

Boxing is implicit (ngầm định); unboxing is explicit (tường minh) phải chỉ rõ data type.

The concept of boxing and unboxing underlies the C# unified view of the type system in which a
value of any type can be treated as an object.
Unboxing is an explicit conversion from the type object to a value type or from an interface type to a
value type that implements the interface.

An unboxing operation consists of:

 Checking the object instance to make sure that it is a boxed value of the given value type.
 Copying the value from the instance into the value-type variable.
So sánh kiểu object, var, dynamic
CONSTANT
A constant has a fixed value that remains unchanged throughout the program while a literal provides
a mean of expressing specific values in a program.

 In C#, you can declare constants for all data types.


 You have to initialize a constant at the time of its declaration.
 Constants are declared for value types rather than for reference types.
 To declare an identifier as a constant, the const keyword is used in the identifier declaration.
The compiler can identify constants at the time of compilation, because of the const
keyword.
Array
An array always stores values of a single data type.

 Each value is referred to as an element.

These elements are accessed using subscripts or index numbers that determine the position of the
element in the array list.

C# supports zero-based index values in an array.

 This means that the first array element has an index number zero while the last element has
an index number n-1, where n stands for the total number of elements in the array.

This arrangement of storing values helps in efficient storage of data, easy sorting of data, and easy
tracking of the data length.

Arrays are reference type variables whose creation involves two steps:

Declaration:

 An array declaration specifies the type of data that it can hold and an identifier.
 This identifier is basically an array name and is used with a subscript to retrieve or set the
data value at that location.

Memory allocation:

 Declaring an array does not allocate memory to the array.


Type of array

Rectangular array
A rectangular array is a n-dimensional array where size of each dimension are equal.

It usually used to represent matrix. It’s very important to help to resolve math problems.

Example:

 Linear Transformations
 Matrix-Vector Multiplication Algorithms
 Gaussian Elimination
 LU Factorization
 The Inverse Matrix

Jagged Array
 Is a multi-dimensional array and is referred to as an array of arrays.
 Consists of multiple arrays where the number of elements within each array can be
different. Thus, rows of jagged arrays can have different number of columns.
 Optimizes the memory utilization and performance because navigating and accessing
elements in a jagged array is quicker as compared to other multi-dimensional arrays.

You might also like