当前位置:网站首页>Enumerations and structures in C #
Enumerations and structures in C #
2022-04-22 07:50:00 【Dream back to Datang knock code】
enumeration :
1. Enumeration
use enum Keyword modification , Add enumeration name
enum Enum name
{
value 1,
value 2,
value 3,
````
}
2. When to use enumerations
Use enumeration when determining the number and value of values , Development of standardized procedures
3. Enumeration assignment
Enum name Variable name = Enum name . value ;
Day nowDay = Day. Monday ;
Structure :
Declaration of a structure use struct Keyword modification , Add structure name
The use of structures :
Different types of data can be stored in the structure , Generally, the structure is used when each individual has some attributes in common
struct
Student
{
Variable type Variable name ;
Variable type Variable name ;
}
Methods in structures :
Methods in structures
<1> If the method is static, you cannot use non static attributes in the structure
<2> If the method is non static, you can make the static and non static attributes in the structure
<3> Calling a static method requires " Structure name . Method name " To call , Non static methods call directly with variable names
<4> Calling a static property requires " Structure name . Property name " To call , Non static attributes call directly with variable names
Code example
using System;
using System.Reflection;
namespace Chapter 10 enumeration and structure
{
#region enumeration
// Enumeration use enum Keyword modification , Add enumeration name
// The default stored in the enumeration type is int Type of , And the following value is always the previous value plus one ;
enum
Day :
int
// The enumeration name can be followed by a limit , But the type of restriction can only be plastic int ,long ,short etc. ;
{
Monday =0,
Tuesday =1,
// The value in the enumeration can be changed by an equal sign
Wednesday ,
Thursday ,
Friday ,
Saturday ,
Sunday
}
#endregion
#region Structure
// Declaration of a structure use struct Keyword modification , Add structure name
// Different types of data can be stored in the structure , It's usually
struct
Student
{
public
string name;
public Xingb xingbie;
// Structs can be used with enumerations
public
int age;
public
string banji;
public
bool youden;
}
enum
Xingb
{
male ,
Woman
}
#endregion
class
Program
{
#region Methods in structures
// Define a structure
struct
Teacher
{
public
static
string name;
public
int age;
// Non static methods can use static properties
public
void Jieshao()
{
Console.WriteLine(
" Hello , I am a :"+name+
" I this year "+age);
}
// Non static properties cannot be used in static methods
public
static
void Kecheng()
{
Console.WriteLine(
" I'll teach you Chinese "+name);
}
}
#endregion
static
void Main(
string[] args)
{
#region enumeration
// Xiao Fei often travels , But every time I go out, I'm always tangled about the date ,
// Use enumeration to help Xiaofei randomly choose the day to travel
Random _ran =
new Random();
// Because enumeration starts from 0 Start , Followed by the preceding number plus 1, So the value is 0 To 7, But does not include 7.
int _numDay = _ran.Next(0,7);
#region Enumeration and int Conversion of types
Day nowDay = Day. Monday ;
// Enumeration types can be converted to int type
int num = (
int)nowDay;
// Because the enumeration type is integer by default , And the first value is from 0 Start , So Monday turns into int The type value is 0
Console.WriteLine(num);
//int Convert to enumeration type ( Here we use random numbers to determine Xiaofei's travel time )
Day _nowDay1 = (Day)_numDay;
//int When converting enumeration, pay attention to whether the value in enumeration exists
// Print Xiaofei's travel time
Console.WriteLine(_nowDay1 );
#endregion
#region Enumeration type and string Conversion between types
// Convert enumeration to string
Day strDay = Day. Wednesday ;
// Enumeration can use .ToString(); Method
string _strDay1 = strDay.ToString();
Console.WriteLine(_strDay1);
// take string Type is converted to enumeration
// Here we use the random number above , First convert the random number into string, Easy to learn string Convert to enumeration
string _strDay2 = _numDay.ToString();
//string There was a Parse Method can string Convert to enumeration
// This method requires two parameters , One is the type , One is string Variable of type
// We need to get this enumeration type Use here typeof( type ) Method to get the enumeration type
// The following code just converts the type to enumeration , But we need to convert to Day Type used
// So I added a strong turn
Day str3NowDay = (Day)Enum.Parse(
typeof(Day),_strDay2);
// The string in the above code can be the string value in the enumeration Such as " Monday ", If the string is a numeric value , There is no... In the enumeration
// The numerical value will not report an error , But if it is " Monday " This type of value , If there is no in the enumeration, an exception will be reported
// The printed result is the same as above
Console.WriteLine(str3NowDay);
#endregion
#endregion
#region Structure
// To use a structure, first declare a structure type
Student stu1;
// Assign a value to the structure , The variable in the assignment structure outside the structure must be Public( Open ) Embellished
stu1.name =
" Zhang San ";
stu1.age = 16;
stu1.xingbie = Xingb. male ;
stu1.banji =
" Class 1, grade 6 ";
stu1.youden =
false;
Student stu2;
stu2.name =
" Li Hong ";
stu2.age = 15;
stu2.xingbie = Xingb. Woman ;
stu2.banji =
" Class 1, grade 6 ";
stu2.youden =
true;
// Print Li Hong's information
Console.WriteLine(
" full name :"+ stu2.name+
"\n Age :" +
""+ stu2.age+
"\n Gender :"+ stu2.xingbie+
"\n Top students :"
+ stu2.youden+
"\n class :"+ stu2.banji);
#region Use of methods in structures
Teacher ter1;
// Static properties, like static methods, need to be called with a structure name
Teacher.name =
" Yang Xue ";
// Non static properties and methods call directly with variable names
ter1.age = 21;
ter1.Jieshao();
Teacher.Kecheng();
#endregion
#endregion
}
}
}
版权声明
本文为[Dream back to Datang knock code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621527070.html
边栏推荐
猜你喜欢
随机推荐
MySQL 中文字段排序问题(根据中文拼音排序)
UML类图
可视化编程——绘图篇作业
Unity知识点(UGUI)
可视化编程——实验二
【TCP/IP 一 概述】
跳转语句的三种
C#变量的使用及注意事项
Pymol用法
Crawler learning 2 - requests module - get request mode
unity面試題
MySQL configuration query
unity面试题附一个(网上商城)
js WdatePicker获取选中的日期
Stm32外设篇 [三] 串口 RS232 RS485
1: Ask what is a thread pool and how to answer it best
Nonparametric structure and fully parametric structure
PWA 我来了
Unity面试题(后续)
数据库增删改查


![Rt-thread [三] link.lds链接脚本详解](/img/80/d62360d0a281b89dcfff61cb2f21ce.png)



![Stm32外设篇 [三] 串口 RS232 RS485](/img/47/d8c8bbe67559d01cde7a118d2dde78.png)


![单片机原理[一] 学好单片机必会的五张图](/img/49/bf1cdb477622375dd558e4c6b1cf60.jpg)