C# defines global variables
-
The following is an example of writing a global variable. Note that the global variable needs to be static
-
public static string[] coffees = new string[] { "Espresso"."Blended coffee"."Reroasted coffee" }; public static int[] coffeePrice = new int[] { 25.30.20 }; public static string[] others = new string[] { "Mocha"."Milk bubble"."Milk" }; public static int[] otherPrice = new int[] { 10.8.6 }; Copy the code
-
An array of
-
Define an array array
-
Indefinite length
- The new operator can be omitted if an initial setting is provided
Int [] Numbers = new int [] {1, 2, 3, 4, 5};
Int [] Numbers = {1, 2, 3, 4, 5};
-
Fixed-length writing
Int [] Numbers = new int [3] {1, 2, 3};
-
Multidimensional array definition
- Indeterminate multidimensional array
Int [and] Numbers = new int [and] {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
- Fixed-length multidimensional array
Int [and] Numbers = new int [3, 3] {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
-
The initialization method is used
-
// If the value is assigned first, the value is 0, and the value is 0 int [] numbers=new int[3]; // This definition method assignment notation numbers[0] =1; Copy the code
-
-
-
Defining a string array
- Indefinite length
- The new operator can be omitted if an initial setting is provided
string[] str={"A","B","C"};
string[] str=new string[]{"A","B","C"};
- Fixed-length writing
string[] str=new string[3]{"A","B","C"};
- Indefinite length
-
Defining an array of objects
Object[] obj = new Object[] { 1, 2, 3, 4, 5 };
C# gets the variable type
-
An example of getting a variable type is as follows:
-
// The first display method System.Type t=123.GetType(); Console.WriteLine(t); // The second method Console.WriteLine(123.GetType()); Copy the code
-
Try to transform
- TryParse can be used to solve the embarrassing problem that many applications will crash due to unsuccessful conversions.
- Example: Try a string variable
str
Try converting to a numeric variablenumbers
.numbers
andstr
All need to be declared before calling; int.TryParse(str,out numbers);
- Note that it has a return value, its return value is a Boolean value, true on success, false otherwise;
number
If the conversion succeeds, the corresponding digit is 0; if the conversion fails, the corresponding digit is 0.
- Note that it has a return value, its return value is a Boolean value, true on success, false otherwise;
- If you want to learn more about TryParse, I have attached a link to TryParse in Depth
- Example: Try a string variable
The pause
Console.ReadLine()
; When the program runs to this point, the program stops automatically.
This note is only when I am writing the coffee shop decorator mode, I am not proficient in the part of the code, attached has been uploaded to GitHub decorator mode code:
Simple coffee shop billing function completed by console application