1024programmer Asp.Net C# study notes–complex data types, functions and structures

C# study notes–complex data types, functions and structures

C# study notes–complex data types, functions and structures

Basic knowledge of C# language. After learning and practicing the introductory knowledge of C#, learn and practice the basic knowledge of C# language!
Involves the basics of the language—some complex data types, as well as classes and structures. Out of simple small program code snippets,
Begin to gradually move towards the abstract world of data. come on!

C# basic

Complex data type

Features: A collection of multiple data variables that can be named by yourself

Types: enumerations, arrays and structures

  • Enumeration: a collection of integer constants
  • Array: A collection of data stored in sequence of any variable type
  • Structure: a data block composed of data of any variable type

enum:

Enumerations can conveniently represent various states of objects, and are essentially variables.

For example, we can use enumerations to represent the type of monster and the player’s action state (stationary, fighting, injured…)

Declaration of enumeration:

enum E_MonsterType//Name E_XXX
 {
      Normal,//0
      Boss,//1 automatically postpones based on the previous value
 }
 enum E_PlayerType
 {
      Main,
      Other,
 }
 

Usage of enumeration types:

//Customized enumeration type variable name = default value; (Customized enumeration type. Enumeration item)
 E_PlayerType playerType = E_PlayerType.Other;

 if(playerType == E_PlayerType.Main)
 {
     Console.WriteLine("Main player logic");
 }
 else if(playerType == E_PlayerType.Other)
 {
     Console.WriteLine("Other player logic");
 }
 //Enumerations are often combined with switch statements

  //Enumeration and switch are a natural match
 E_MonsterType monsterType = E_MonsterType.Boss;
 switch(monsterType)
 {
     case E_MonsterType.Normal:
         Console.WriteLine("Ordinary monster logic");
         break;
     case E_MonsterType.Boss:
         Console.WriteLine("Boss logic");
         break;
     default:
         break;
 }
 

Enumeration type conversion:

  • Conversion between enumeration and int:
int i=(int) playerType;
 playerType=0;
 
  • Conversion between enumeration and string:
playerType=(E_PlayerType)Enum.Paese(typeof(E_PlayerType),"other");
 string str=playerType.ToSring();
 

array:

An array is an ordered collection of data that stores the same specific variable type, and is a continuous storage in memory.

One-dimensional array

//Array declaration
 int[] arr1;//Unallocated memory address
 int[] arr2=new int[5];//Element default value 0
 int[] arr3=new int[5]{1,2,3,4,5};
 int[] arr4=new int[]{1,2,3};
 int[] arr5={1,2,3,4,5,6};

 //Usage of array
 arr2.Length;//Length
 arr2[0];//Get the elements in the array
 arr2[2]=99;//Modify content
 //Array traversal
 for(int i=0;i<arr2.Length;i++)
 {
     //Traverse
      Console.WriteLine(arr2[i]);
 }

 //Custom expansion of array
 //Declare a new large-capacity array and copy and transfer the old one
 int[] array2 = new int[6];
 //move place
 for (int i = 0; i < array.Length; i++)
 {
     array2[i] = array[i];
 }
 array = array2;

 //Customize the implementation of deleting elements of the array
 int[] array3 = new int[5];
 //Move transfer
 for (int i = 0; i < array3.Length; i++)
 {
     array3[i] = array[i];
 }
 array = array3;


 //Find elements in array
 //Only traverse the array and terminate the traversal if the appropriate one is found.
 int a = 3;
 for (int i = 0; i < array.Length; i++)
 {
     if( a == array[i] )
     {
         Console.WriteLine("The element equal to a is at index position {0}", i);
         break;
     }
 }

 

Multidimensional array (two-dimensional array)

//Two-dimensional array
 //statement
 int[,] arr;
 int[,] arr2=new int[3,4];
 int[,] arr3=new int[3,4]{{0,1,2,3},//Give the element content and tell the dimension the number of elements
                          {0,1,2,3},
                          {0,1,2,3}};
 int[,] arr4=new int[,]{{0,1,2,3},//Give the element content and tell the dimension
                          {0,1,2,3},
                          {0,1,2,3}};
 int[,] arr5={{0,1,2,3}, //Give the element content directly
              {0,1,2,3},
              {0,1,2,3}};

 //use
 //Get the length
 arr3.GetLength(0);//The length of the line
 arr3.GetLength(1);//The length of the column

 //Get elements
 arr3[1,1];//Elements of the first row and first column
 //Traverse the two-dimensional array
 for(int i=0;i<arr3.GetLength(0);i++)
 {
     for(int j=0;j<arr3.GetLength(1);j++)
     {
         //Access specific elements
     }
 }
 //Add array elements
 //Idea: Create a large-capacity array and copy the small array elements to the large array
 //Finally modify the small array index to point to the large array to achieve "array expansion"
 //Delete array elements (same as adding)
 //Find elements in the array (traverse comparison)
 

Jagged array

Jagged arrays are different from multi-dimensional arrays in that they are arrays of arrays. One-dimensional array elements are also arrays, and their lengths do not have to be exactly the same.

//interleaved array
 //statement
 int [][] arr1;
 intvalue = 3;
     }
     static void ChangeArrayOut(out int[] arr)
     {
         arr = new int[] { 666, 777, 888, 999 };
     }
 //Add to Main function

  int a1 = 1;
             Console.WriteLine("a1 original value {0}", a1);
             ChangeValue(a1);
             Console.WriteLine("The value of a1 after calling the ChangValue function {0}", a1); //The value has not changed
             ChangeValueOut(out a1);
             Console.WriteLine("The value of a1 after a1 calls the ChangValueOut function {0}", a1); //Change the value
             Console.WriteLine("************************************");
             int[] arr1 = { 100, 200, 300 };
             Console.WriteLine("arr1[0]original value{0}", arr1[0]);
             ChangeArray(arr1);
             Console.WriteLine("The value of arr1[0] after calling the ChangArray function {0}", arr1[0]); //The value has not changed
             ChangeArrayOut(out arr1);
             Console.WriteLine("The value of arr1[0] after calling the ChangArrayOut function {0}", arr1[0]);//Change the value

 

image

Difference:

  1. The variable passed in by ref must be initialized and out does not have to
  2. The variable passed in by out must be assigned internally, ref does not need to be

Understanding: ref already has an initial value when it is passed in, so it does not need to be modified internally,

​ out may not have an initial value when it is passed in, so to ensure it is valid, it must be assigned internally.

Variable-length parameter keyword of function —params

  1. It can only be followed by an array
  2. The array type can be any type
  3. Only oneparams can appear in the function parameter list, and the modified parameters can only appear at the end
//Function variable length parameter keyword params must be an array after
 //Find the sum of n ints
 int Sum(params int[] arr)
 {
     int sum=0;
     for(int i=0;i<arr.Length;i++)
     {
         sum+=arr[i];
     }
     return sum;
 }
 void Chat(string name,params string[] things)
 {
     //Function body...
 }
 

Function parameter default value

There can be multiple parameters in the function parameter list. Parameters with default values ​​must be after ordinary parameters

void Speak(string thing="I have nothing more to say")
 {
     Console.WriteLine(thing);
 }
 //transfer
 Speak();
 Speak("I want to say,,,,");
 

Function overloading:

In the same statement block, the function name is the same, but the parameter list of the function is different (the parameter type is different or the number of parameters is different, if both are the same, the parameter order is different)

//Function overloading
 //The number of parameters of the function is different
 int Sum(int a,int b)
 {
     return a+b;
 }
 int Sum(int a,int b,int c)
 {
     return a+b+c;
 }
 // Parameters have the same number but different types
 float Sum(int a,int b)
 {
     return a+b;
 }
 float Sum(float f1,float f2)
 {
     return f1+f2;
 }
 //Parameters have the same number, same type, but different order
 float Sum(float f,int i)
 {
     return f+i;
 }
 float Sum(int i,float f)
 {
     return f+i;
 }
 //ref and out change parameter types and constitute overloading with ordinary functions
 //ref and out are counted as one class and cannot constitute overloads of each other.
 int Sum(int a,int b)
 {
     return a+b;
 }
 int Sum(ref int a,int b)
 {
     return a+b;
 }
 //params can be used as overloads
 //But parameter default values ​​cannot constitute overloading
 

recursive function

The function calls itself, and recursion has a recursive base and conditions for ending the call.

//Recursively print 0-10
 void Fun(int a)
 {
     if(a>10)
         return;
     Console.WriteLine(a);
     a++;
     Fun(a);
 }
 //Recursion----find the factorial of a certain number
 int getFactory(int a)
 {
     //end condition
     if(a==1)
         return 1;
     //recursive base
     return a*getFactory(a-1);
 }
 //Recursion----find 1!  +2!  +3!  +···+10!
 int getFactorySum(int num)
 {
     if(num==1)
         return 1;
     return getFactorial(num)+getFactorial(num-1);
 }
 //recursion---
 //A bamboo pole is 100m long and is cut in half every day. What is the length on the tenth day?
 void getLong(float length,int day=0)
 {
     length/=2;
     if(day==10)
     {
         Console.WriteLine("The length of the bamboo after cutting on the tenth day is {0} meters", length);
     }
     ++day;
     getLong(length,day);
 }
 //---Recursively print 1-200
 //Recursion + short circuit
 bool Fun5(int num)
 {
     Console.WriteLine(num);
     return num==200||Fun5(num+1);//The first one is true and will be short-circuited
 }
 

structure

A structure is a collection of data and methods, in namespace

Naming rules: Pascal nomenclature (first letters are all capitalized)

Constructor:

  1. No return value
  2. Must be the same as the structure name
  3. Must have parameters
  4. If there is a constructor, all variables must be assigned
//Structure
 struct Student
 {
     //Cannot contain structures of its own type--Student s; Error!
     //Name:
     public string name;
     //age
     public int age;//Numeric variables cannot be initialized directly
     public bool sex;
     //Constructor (there is a parameterless constructor by default. If there is a constructor, the default constructor will not fail, which is different from classes)
     //Initialize variables
     public Student(string name,int age,bool sex)
     {
         this.name=name;
         this.age=age;
         this.sex=sex;
     }
     //method
     public void Speak()
     {
         Console.WriteLine("Hello");
     }
 }

 //Use of structure
 Student s1;
 s1.name="Tony";
 s1.age=18;
 s1.sex=true;
 s1.Speak();
 

Access modifier

  1. public
  2. private (not written as private by default)
  3. Protect inherited can be accessed
  4. internal – internal types or members can only be accessed in files in the same assembly

Now that you have seen this, let me give you a 👍!
A small thumbs up is a great encouragement to the author!
Follow me, I will update C# study notes every day, from entry to advanced, let’s learn together! ! !

��cannot be initialized directly
public bool sex;
//Constructor (there is a parameterless constructor by default. If there is a constructor, the default constructor will not fail, which is different from classes)
//Initialize variables
public Student(string name,int age,bool sex)
{
this.name=name;
this.age=age;
this.sex=sex;
}
//method
public void Speak()
{
Console.WriteLine(“Hello”);
}
}

//Use of structure
Student s1;
s1.name=”Tony”;
s1.age=18;
s1.sex=true;
s1.Speak();

Access modifier

  1. public
  2. private (not written as private by default)
  3. Protect inherited can be accessed
  4. internal – internal types or members can only be accessed in files in the same assembly

Now that you have seen this, let me give you a 👍!
A small thumbs up is a great encouragement to the author!
Follow me, I will update C# study notes every day, from entry to advanced, let’s learn together! ! !

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811505

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索