One, enumeration
What is enumeration?
Enumeration is a set of named integer constants. Enumeration types are declared using the enum keyword. That is, some fixed range of values.
C# enumerations are value types. Enumerations contain their own values and cannot be inherited or passed on
What are the benefits of using enums?
①Save memory space . For example, now we need to store some information in the data table: 1 represents apple, 2 represents peach, 3 represents watermelon, the three words of apple are all string types, 1 these three Arabic numbers are all char type, char type Save space more than string type;
②Easy to use . You can order whoever you want;
③More standardized ;
④ Strong conversion between types can be performed and output in different forms (each symbol represents an integer value, arranged in order, starting from 0 by default).
How to declare enumeration?
grammar:
[public] enum <sheet type type name>
{
Value 1,
Value 2
};
Type conversion between enumeration values:
public enum Gender //声明枚举类型 public访问修饰符,有一些固定的范围的值
{
男,
女
}
class Program
{
static void Main(string[] args)
{
Gender gender = Gender.女;
int gender = (int)Gender.女; //enum——>int
Gender gender = (Gender )1; //int——>enum
string num = gender.ToString(); //string——>enum
Gender gender = (Gender)Enum.Parse(typeof(Gender), "女"); //enum——>string
Console.WriteLine(num); //显示枚举值
Console.ReadKey();
}
}
Second, the structure
What is the structure?
It enables a single variable to store related data of various data types. struct keyword to create
Why use structures (benefits)?
Declare multiple variables of different types at once . For example, if a person needs variables such as name, age, and gender, we need to declare it three times. If there are thousands of people, should we declare thousands of different variables? Isn't this variable redundant? The structure reduces this situation, declare a structure, which can have variables of different data types
Note: Structure is a value type, structure members cannot be specified as abstract, virtual, protected, and structure does not support inheritance
The fields in the structure cannot be assigned initial values; the structure is stored in the stack
Declaration statement
[public] struct structure name
{
Structure member
}
public enum Gender //声明枚举
{
男, //值
女
}
public struct Person //声明结构体
{
public string _name; //字段,姓名 ,结构体成员
public Gender _gender; //性别
public int _age; //年龄
}
static void Main(string[] args)
{
Person XDperson; //声明XDperson,类型为Person
XDperson._name = "小邓";
XDperson._age = 18;
XDperson._gender = Gender.男;
Person XJperson; //声明XDperson,类型为Person
XJperson._name = "小姜";
XJperson._age = 18;
XJperson._gender = Gender.女;
Console.WriteLine(XDperson._name); //打印XDperson的信息
Console.WriteLine(XDperson._age );
Console.WriteLine(XDperson._gender);
Console.WriteLine(); //打印一行空行
Console.WriteLine(XJperson._name); //打印XDperson的信息
Console.WriteLine(XJperson._age);
Console.WriteLine(XJperson._gender);
Console.ReadKey();
}