C# basics (complex data structures)

1. Constant

Syntax for declared constants: const variable type variable name = value;

Constants cannot be assigned repeatedly

const int number = 10;

2. Enumeration

1) Grammar

[public] enum name ·

{

value 1,

value 2,

value 3,

........

}

public: access modifier. It is public and can be accessed anywhere.

enum: keyword, keyword to declare an enumeration

Enumeration name: must comply with Pascal naming convention

Declare the enumeration below the namespace and outside the class, which means that all classes in this namespace can use this enumeration.

An enumeration is a variable type, int--double string decimal. It's just that the way enumerations are declared, assigned, and used is different from those of ordinary variable types.

//枚举类型定义
public enum Gender
    {
        男,
        女
    }
    
//使用
Gender gender = Gender.男;

2) Enumeration conversion

We can convert an enumeration type variable to int type and string type and back.

Enumeration types are compatible with int types by default, so they can be converted to each other through cast syntax.

//枚举类型的数字是默认从0开始的

Gender gender = Gender.男;
int n = (int)gender;//这里n是0

//有值时转换为枚举类型
//当转换一个枚举中没有的值的时候,不会抛异常,而是直接将数字显示出来。
int n1 = 2;
Gender g = (Gender)n1;

Enumerations can also be converted to and from string types. If the enumeration type is converted to the string type, ToString() is called directly. If the string is converted to the enumeration type, the following line of code is required:

(Enumeration type to be converted) Enum.Parse(typeof (Enumeration type to be converted), "String to be converted");

If the converted string is a number, no exception will be thrown even if it is not found in the enumeration.

If the converted string is text, an exception will be thrown if it is not present in the enumeration.

//枚举转换为字符串
Gender gender = Gender.男;
string s = gender.ToString();//s的值为"男"


//字符串转换为枚举
string s = "0";
Gender g = (Gender)Enum.Parse(typeof(Gender),s);

3. All types can be converted to string type by calling ToString().

4. Structure

It can help us declare multiple variables of different types at one time.

Syntax: [public] struct structure name

{

member; //field

}

Variables can only store one value while the program is running, while fields can store multiple values.

//定义
//字段前面要加下划线_
public struct Person
    {
        public string _name { get; set; }
        public int _age { get; set; }
        public string _address { get; set; }
    }

//使用
Person zsPerson;
zsPerson._name = "张三";

5. Array

Store multiple variables of the same type at once.

Syntax: array type[] array name=new array type[array length];

***Once the length of an array is fixed, it cannot be changed.

//数组的四种写法,推荐1、2种
int[] nums = new int[10];
int[] numsTwo = {1,2,3}
int[] numsThree = new int[]{1,2,3}
int[] numsThree = new int[3]{1,2,3}

6. Bubble sorting

It is to arrange the elements in an array in order from large to small or small to large.

int[] nums={9,8,7,6,5,4,3,2,1,0}; 0 1 2 3 4 5 6 7 8 9

The first comparison: 8 7 6 5 4 3 2 1 0 9 exchanged 9 times i=0 j=nums.Length-1-i

The second comparison: 7 6 5 4 3 2 1 0 8 9 exchanged 8 times i=1 j=nums.Length-1-i

The third comparison: 6 5 4 3 2 1 0 7 8 9 exchanged 7 times i=2 j=nums.Length-1-i

The fourth comparison: 5 4 3 2 1 0 6 7 8 9 exchanged 6 times i=3 j=nums.Length-1-i

The fifth comparison: 4 3 2 1 0 5 6 7 8 9 exchanged 5 times

The sixth comparison: 3 2 1 0 4 5 6 7 8 9 exchanged 4 times

The seventh comparison: 2 1 0 3 4 5 6 7 8 9 exchanged 3 times

The eighth comparison: 1 0 2 3 4 5 6 7 8 9 exchanged 2 times

The ninth comparison: 0 1 2 3 4 5 6 7 8 9 exchanged 1 time

for(int i=0;i<number.Length-1;i++) 
{  
    for(int j=0;j<nums.Length-1-i;j++)  
    {   
        if(nums[j]>nums[j+1])  
         {   
             int temp=nums[j];    
             nums[j]=nums[j+1];    
             nums[j+1]=temp;   
         } 
    }
}

7. Method

Functions are a mechanism for reusing a bunch of code.

Function syntax: [public] static return value type method name ([parameter list]) { method body; }

public: Access modifier, public, public, accessible anywhere.

static: static

Return value type: If there is no need to write a return value, write void

Method name: Pascal The first letter of each word is larger. The rest of the letters are lowercase

Parameter list: The conditions that must be provided to this method to complete this method. If there are no parameters, the parentheses cannot be omitted.

After the method is written, if it wants to be executed, it must be called in the Main() function.

Method calling syntax: class name.method name([parameter]);

***In some cases, the class name can be omitted. If the method you write is in the same class as the Main() function, then the class name can be omitted.

8、return

1). Return the value to be returned in the method.

2) End this method immediately.

Guess you like

Origin blog.csdn.net/m0_55074196/article/details/126697834