Hello everyone, I am denylau. Today I am very depressed. I have already sorted out a lot of questions about variables, and I am about to finish it. The computer is abnormal and everything is done in vain, I was wondering why I couldn't save so many things I wrote before posting my blog? Once a problem occurs in the browser, all is in vain. I hope that a temporary storage function will be added to my blog in the future, you can also view it directly when you close the browser and open it again in time. Good, draft is relatively troublesome.
Now, let's repeat the variables!
1. Concepts of Variables
The variable itself is used to store specific types of data and can change the data values stored in the variable at any time as needed. A variable has a name, type, and value.
After learning about the concept of variables, what types of variables do you really want to know? In C #, there are two types of variables: Value Type and reference type. So what are their direct differences? The Value Type stores the data of the variable, while the reference type stores the reference of the actual data. The following describes the features of the value type and reference type in detail.
Value Type:
· Value Type variables are stored in the stack.
· When you access a value type variable, you generally directly access its instance.
· Each value type variable has its own data copy, so operations on a value type variable will not affect other variables.
· When copying a value type variable, it copies the value of the variable instead of the address of the variable.
· A value type variable cannot be null and must have a definite value.
Reference Type:
· The memory must be allocated to the referenced type variable in the managed heap.
· You must use the new keyword to create a reference type variable.
· Each object allocated in the managed heap has associated additional members, which must be initialized.
· The reference type variables are managed by the garbage collector.
· Multiple referenced type variables can reference the same object. In this case, operations on one variable may affect the same object referenced by another variable.
· All values before the reference type is assigned a value are null.
In the process of programming, we often use a special value type which is an enumeration type. What are the characteristics of this type?
Enumeration type: the enumeration type is used to declare a group of variables of the same nature. The Enumeration type can increase the readability and maintainability of the program. At the same time, the enumeration type can avoid type errors. Next I will introduce the enumeration type to you through an instance:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Test03
{
Class Program
{
Enum MyDate
{
Sun = 0,
Mon = 1,
Tue = 2,
Wed = 3,
Thi = 4,
Fri = 5,
Sat = 6
}
Static void Main (string [] args)
{
Int k = (int) DateTime. Now. DayOfWeek;
Switch (k)
{
Case (int) MyDate. Sun: Console. WriteLine ("Today is Sunday"); break;
Case (int) MyDate. Mon: Console. WriteLine ("Today is Monday"); break;
Case (int) MyDate. Tue: Console. WriteLine ("Today is Tuesday"); break;
Case (int) MyDate. Wed: Console. WriteLine ("Today is Wednesday"); break;
Case (int) MyDate. Thi: Console. WriteLine ("Today is Thursday"); break;
Case (int) MyDate. Fri: Console. WriteLine ("Today is Friday"); break;
Case (int) MyDate. Sat: Console. WriteLine ("Today is Saturday"); break;
}
Console. ReadLine ();
}
}
}
The program first uses the enum keyword to create an enumeration, and then declares an int type variable K, which is used to obtain the day of the week currently expressed, and finally calls the switch statement to output the week.
Since there are more than one data type in C #, or there are many data types, can these data types be converted? If yes, what conversion methods do they have?
Two types of conversions: implicit conversion and explicit conversion
Stealth conversion: the conversion can be performed without declaration. The common point is to convert the data type in a small range to a large range of data types.
Explicit conversions: This is what we often call forced conversions. You need to specify the data type to be converted in the code. The general saying is to convert from a large range to a small data type.
What about the conversion between the value type and the reference type?
So we have to talk about packing and unpacking.
Packing: the process of converting a value type to a reference type is called packing. It is a special implicit conversion.
Unpacking: converts the display of reference types to the value type, which is a special case of explicit conversion.