Game Development Research Phase I C# Basic Grammar

Table of contents

1. The components of C#

1. Namespace

2. class

3. Main method

4. Class method (member function)

4.1 Class Method Classification

4.2 Parameter passing of class methods

5. Member variables, local variables, constants

5.1 Member variables

5.2 Local variables

5.3 Constants

6. Statements and expressions

Features:

7. Notes

8. Identifiers

9. Keywords

10. Operators

10.1 Arithmetic operators [7 types]

10.2 Relational operators [6 types]

10.3 Logical operators [3 types]

10.4 Bitwise operators [6 types]

10.5 Assignment operators [11 types]

10.6 Other operators [8 types]

10.7 Operator precedence

2. Data type

1. Value type

2. Reference type

2.1 Built-in reference data types

2.2 User-defined reference types

3. Pointer type

4. Nullable types

4.1 Single question mark

2.4.2 Double question mark [??]

3. Data type structure

1. Array

1.1 Concept

1.2 use

2. Enumeration

2.1 Statement

2.3 Integers represented by enumerated lists

2.4 Conversion of enumeration and int

2.5 Conversion of enumeration and String

3. String

3.1 Method of creating String object

3.2 Properties of the String class

3.3 String class methods

1. The components of C#

1. Namespace

A namespace contains many classes.

//Use the using keyword to refer to the namespace
using System;
namespace HelloWorldApplication
{
    //kind
    class HelloWorld
    {
        //Main method - the execution entry of the program
        public static void Main(string[] args)
        {
            //Used to stay in the console, waiting for the user to press any key
            Console.ReadKey();
        }
    }

2. class

A class contains multiple class methods and class attributes, and the class can be instantiated.

//Use the class keyword to declare a class
using System;
namespace HelloWorldApplication
{
    //HelloWorld class
    class HelloWorld
    {

        
    }
	//Entrance class
	class Entrance
    {
        static void Main()
        {
			//class instantiation        
            HelloWorld identifier = new HelloWorld();
            //Used to stay in the console, waiting for the user to press any key
            Console.ReadKey();
        }   
    }
    
}

3. Main method

The Main function is the entry point of the program.

//The Main method is the entry point of the program
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        static void Main(String[] args)
        {
            Console.WriteLine("Main The method is the entry point of the program");
            Console.ReadKey();
        }
    }
}

output:

Main The method is the entry point of the program

4. Class method (member function)

A block of statements used to perform a task.

//Multiple class methods in a class
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //first class method
        public void Function_1()
        {
            

        }
        //second class method
        public void Function_2()
        {


        }
        //Main method - the execution entry of the program
        public static void Main(String[] args)
        {
            //Output the content in parentheses to the console, and wrap at the last position (can have no parameters, print a blank line)
            Console.WriteLine("HelloWorld!");
            Console.ReadKey();
        }
    }
}

output:

HelloWorld!

4.1 Class Method Classification

(1) According to the definition: return value, no return value

(2) According to the call: with parameters, without parameters, recursive

Code Demo·Recursive Function

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //factorial of 10
        public int Function(int num)
        {
            int result;
            if (num == 1)
            {
                return 1;
            }
            else
            {
                //self call
                result = Function(num - 1) * num;
                return result;
            }
        }
    }

    class MyMain
    {
            static void Main(string[] args)
        {
            //object instantiation
            HelloWorld hello = new HelloWorld();
            //Call class method using instance object
            Console.WriteLine(hello.Function(10));
            Console.ReadKey();
        }
    }
}

output:

3628800

4.2 Parameter passing of class methods

Wayprinciple
value parameterAssign the value of the actual parameter to a newly created formal parameter, and there are two different areas in the memory. Changing the formal parameters does not change the actual parameters.
reference parameterRefer to the memory location to the formal parameter. When the formal parameter changes, the actual parameter also changes.
Output parameterscan return multiple values

(1) Code Demo·Value Parameters

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //swap two numbers
        public void Function(int a, int b)
        {
            int temp;
            temp = a;
            a = b;
            b = temp;
        }
    }

    class MyMain
    {
        static void Main(string[] args)
        {
            //Test whether two prints are the same
            int a = 200;
            int b = 300;
            Console.WriteLine("before exchange a:s{0},b:{1}", a, b);
            //object instantiation
            HelloWorld hello = new HelloWorld();
            //Call class method using instance object
            hello.Function(a, b);
            Console.WriteLine("after exchange a:s{0},b:{1}", a, b);
            Console.ReadKey();
        }
    }
}

output:

before exchange a:s200,b:300
 after exchange a:s200,b:300

(2) Code Demonstration · Reference Parameters

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //use the ref keyword
        public void Function(ref int a, ref int b)
        {
            int temp;
            temp = a;
            a = b;
            b = temp;
        }
    }

    class MyMain
    {
        static void Main(String[] args)
        {
            //Test whether two prints are the same
            int a = 200;
            int b = 300;
            Console.WriteLine("before exchange a:s{0},b:{1}", a, b);
            //object instantiation
            HelloWorld hello = new HelloWorld();
            //Call class method using instance object
            hello.Function(ref a, ref b);
            Console.WriteLine("after exchange a:s{0},b:{1}", a, b);
            Console.ReadKey();
        }
    }
}

output:

before exchange a:s200,b:300
 after exchange a:s300,b:200

(3) Code Demonstration·Output Parameters-part 1

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //Use the out keyword to assign a value within a method
        public void Function(out int a, out int b)
        {
            int num1 = 10;
            int num2 = 20;
            a = num1;
            b = num2;
        }
    }

    class MyMain
    {
        static void Main(string[] args)
        {
            //The two prints are not the same
            int a = 200;
            int b = 100;
            Console.WriteLine("Before outputting parameters, a:{0},b:{1}", a, b);
            HelloWorld hello = new HelloWorld();
            hello.Function(out a,out b);
            Console.WriteLine("After outputting the parameters, a:{0},b:{1}", a, b);
            Console.ReadKey();
        }
    }
}

output

Before outputting parameters, a:200,b:100
 After outputting the parameters, a:10,b:20

(4) Code Demonstration·Output Parameters-part 2

//Statement only, assigned by receiving input
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public void Function(out int a, out int b)
        {
            Console.WriteLine("first value");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second value");
            b = Convert.ToInt32(Console.ReadLine());
        }
    }

    class MyMain
    {
        static void Main(string[] args)
        {
            int a, b;
            HelloWorld hello = new HelloWorld();
            hello.Function(out a, out b);
            Console.ReadKey();

        }
    }
}

output:

//Enter 10 and press Enter, enter 20 and press Enter
 Please enter the first value
10
 Please enter a second value
20
 Entered value a:10,b:20

5. Member variables, local variables, constants

Used to store data.

5.1 Member variables

At the same level as the class method, the scope of action is determined by the access modifier.

//Multiple class attributes in a class
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //public modification, the scope is within the entire class
        //Only the member variables are declared here, and no assignment has been made yet
        public int score;
        public string name;

        public static void Main(String[] args)
        {
            Console.ReadKey();
        }
    }
}

output:

 

5.2 Local variables

the name of a memory area for program manipulation;

Located inside the class method, it only takes effect in the class method

//Multiple class attributes in a class
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public void Function()
        {
            //variable
            int c = 1;
            //statement
            int a;
            //initialization
            a = 10;
        }
        public static void Main(string[] args)
        {
            Console.ReadKey();
        }
    }
}

output:

 

5.3 Constants

The value is fixed, and the constant cannot be modified after assignment.

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        //Constants are declared using const and located within the class
        public const float PI = 3.14159f;
        public static void Main(String[] args)
        {
            Console.WriteLine(PI);
            Console.ReadKey();
        }
    }
}

output:

3.14159

(1) integer constant

The prefix is ​​used to specify the base
prefixexpressexample
0x or 0Xhexadecimal0xFeel
0Octal0213
nonedecimal85
The suffix is ​​used to specify the type [can be superimposed]
suffixexpressexample
U or uunsigned30u
L or llong integer45l
Stackable but not repeatableunsigned long integer30lu

(2) Floating point constant

[Integer part]+ . + [Decimal part and integer part]

Divided into decimal representation and exponential representation
decimal representation3.14159
Exponent representation[2.1*10^5]2.1E5

(3) Character constants

escape charactermeaning
\\character
''character
""character
\??character
\abeep
\bBackspace [Backspace]
\fchange page
\nnew line
\rcarriage return
\tHorizontal Tab Tab
\vVertical Tab Tab
\oooOne to three digit octal numbers
\xhh...A hexadecimal number of one or more digits

(4) String constant

Strings enclosed in " " and @" "

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {

        public static void Main(String[] args)
        {
            string a = "hello, world";
            //@" "You can split a very long line into multiple lines, and you can use spaces to separate the parts
            string j = @"one
        two
        three";
            Console.WriteLine("first in\"  \"The characters inside are:\n{0}\n the second in@\"  \"The characters inside are:\n{1}",a,j);
            Console.ReadKey();
        }
    }
}

output:

first in"  "The characters inside are:
hello, world
 the second in@"  "The characters inside are:
one
        two
        three

6. Statements and expressions

Features:

(1) A statement is a piece of executable code, {} is a range, and does not necessarily have a return value;

(2) Expression = operator + operand, the expression always has a return value;

(3) Both end with [;].

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public void Function()
        {
            //expression
            int a = 1;
            //sentence
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }

        public static void Main(string[] args)
        {
            HelloWorld hello = new HelloWorld();
            hello.Function();
        }
    }
}

output:

Hello World!

7. Notes

Comments are ignored by the compiler when compiling

//Divided into single-line comments and multi-line comments
using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            //single line comment

            /*
			*multiline comment
			*/
            Console.ReadKey();
        }
    }
}

output:

 

8. Identifiers

Namespace name, class name, method name, variable name.

It consists of four parts: Arabic numerals 0-9, 26 English letters, _, @, among which Arabic numerals cannot be used as the beginning, and English letters are case-sensitive.

9. Keywords

Identifiers that have been taken up by CTS (Common Language Specification\Typesetting System).

There are two parts, reserved keywords and context keywords.

10. Operators

10.1 Arithmetic operators [7 types]

Addition, subtraction, multiplication and division, modulo, self-addition and self-subtraction

+ - * / % ++ --
//After the ++ operation first, then self-increment
a++;
//First ++ first self-increment,
++a

10.2 Relational operators [6 types]

Equal Not equal Greater than Less than Greater than or equal Less than or equal to.

== != > < >= <=

10.3 Logical operators [3 types]

AND or NOT , both sides can only be operated on Boolean values.

&& || !

10.4 Bitwise operators [6 types]

AND OR EXCLUSIVE OR INVERSION Left shift right shift, both sides need to be able to perform binary operations.

Shift left one bit*2;

Shift right one bit/2;

& | ^ ~ << >>
A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100	//all 1 for 1

A|B = 0011 1101	//1 for 1

A^B = 0011 0001	//same 0 different 1

~A  = 1100 0011	//take the opposite

10.5 Assignment operators [11 types]

Assignment Addition Assignment Subtraction Assignment Multiply Assignment Divide Assignment Modulo Assignment Left Shift Assignment Right Shift Assignment Bitwise AND Assignment Bitwise OR Assignment Bitwise XOR Assignment

= += -= *= \= %= <<= >>= &= |= ^= 

10.6 Other operators [8 types]

Return the size of the data type Return the class value Return the address of the variable The pointer of the variable Conditional expression Determine whether it is the same type Forced conversion [No exception will be thrown if it fails]

operatorexample
sizeof()sizeof(int)
typeof()typeof(System)
&&a
**a
?:b=(a == 10) ? 20:30[Ternary operator]
isif(Ford is Car) [whether the previous instance is an object of the following class]
as

Object obj = new StringReader("Hello");

StringReader r = obj as StringReader;

??Null coalescing operator num1 = num2 ?? num 3;

10.7 Operator precedence

slightly

2. Data type

1. Value type

You can assign a value directly, and the system allocates memory when assigning a value.

typevalueRangesDefaults
boolBoolean valueTrue and FalseFalse
byte8-bit unsigned integer[0,255]0
char16-bit Unicode characters[U +0000,U +ffff]'\0'
decimal128 decimal values, 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
double64-bit double precision floating point(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32-bit single precision floating point-3.4 x 1038 to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
ushort16-bit unsigned integer type0 to 65,5350
Nullableempty typeNullable data typesNULL

Allows defining variables of other value types, such as enum;

Allows the definition of reference type variables, such as class;

2. Reference type

is an address pointing to the memory address of the variable containing the actual data, referencing the value.

2.1 Built-in reference data types

built-in reference data types
object [object type]
dynamic [dynamic type]
string [string type]

(1) Object type

① Object is the ultimate parent class of all data types in CTS [General Data Type System], that is, the System.Object class;

② The object (Object) type can be assigned any other type (value type, reference type, predefined type or user-defined type) value, before assigning a value, type conversion is required;

③ When a value type is converted to an object type, it is called boxing; on the other hand, when an object type is converted to a value type, it is called unboxing.

(2) Dynamic type

Any type of value can be stored. Type checking happens at runtime.

//dynamic type declaration
dynamic d = 20;

(3) String type

It can store any string value, it is the System.String class, derived from the System.Object class.

Assignment of values: " " and @" ".

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        static void Main(String[] args)
        {

            String str_1 = "great!";
            string str_2 = @"great";
            //You can add @ before the string string to treat the escape character () as a normal character
            string str_3 = @"C:\Windows";
            //Equivalent to:
            string str_4 = "C:\\Windows";
            //@ Any newline can be used in the string, and newline characters and indentation spaces are counted within the length of the string.
            string str_5 = @"<script type=""text/javascript"">
    <!--
    -->
</script>";
            Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}",str_1,str_2,str_3,str_4,str_5);
            Console.ReadKey();
        }
    }
}

output:

great!
great
C:\Windows
C:\Windows
<script type="text/javascript">
    <!--
    -->
</script>

2.2 User-defined reference types

user-defined reference type
class [class]
interface [interface]
delegate [pointer]

3. Pointer type

Store another type of memory address.

Pointer declaration:

char* cptr;
int* iptr;

4. Nullable types

Nullable types can represent the range of original values ​​of other basic types + null;

Nullable types are useful when dealing with database data;

4.1 Single question mark

(1) Function

?Used to perform null assignment operations for some data types that cannot be assigned a value of null

//For example, the original range of int is -2,147,483,648 to 2,147,483,647
int num1;
//At this time, the desirable range of num2 becomes -2,147,483,648 to 2,147,483,647, plus null;
int? num2;

After the operation is completed, add null to the original value range, and the default value of the data type becomes null;

//The default value is 0
int num1;
//The default value is null
int? num2;

(2) Implementation principle

Convert the original data type into a Nullable object type through type conversion;

The actual operation is:

int? i = 1;

Equivalent to:

Nullable<int> i = Nullable<int>(1);

(3) Usage

using System;
namespace HelloWorldApplication
{
    class MyMain
    {
        static void Main(string[] args)
        {
            //Output num1, num3, value can be empty
            int? num1 = null;
            int? num2 = 45;
            double? num3 = new double();
            //Equivalent to: Nullable<double> num3 = new Nullable<double>();
            double? num4 = 3.14159;
            //Equivalent to: Nullable<bool> value = new Nullable<bool>();
            bool? value = new bool?();
            Console.WriteLine(num1);
            Console.WriteLine(num2);
            Console.WriteLine(num3);
            Console.WriteLine(num4);
            Console.ReadKey();
        }
    }
}

output:

45
0
3.14159

2.4.2 Double question mark [??]

When the variable being judged is null, another value is returned.

//If num2 is null, assign num3 to num1, and if it is not null, directly assign num2 to num1;
num1 = num2 ?? num3;

3. Data type structure

1. Array

1.1 Concept

An ordered, finite collection of data of the same data type whose memory is linearly contiguous.

1.2 use

(1) Declaration and initialization

The declaration of the array will not open up space in memory, it will only be opened after initialization.

//Array declaration, declare an array named arr whose value type is int;
int[] arr;
//For the initialization of the array, specify the array capacity (length) of arr as 4, and assign the initial value (0) of the int data type to each position, and open up an area in the memory to store the array;
arr = new int[4];
//Array declaration and initialization can be done together;
int[] arr = new int[4];

One-dimensional array initialization

//The first method of initialization: explicitly define the length of the array and then assign it individually
int[] arr1 = new int[5];
arr1[0] = 1;
arr1[1] = 1;
arr1[2] = 1;
arr1[3] = 1;
arr1[4] = 1;
//The second method of initialization: explicitly define the length of the array and assign
int[] arr2 = new int[5] { 1, 2, 3, 4, 5 };
//The third method of initialization: implicit definition direct assignment
int[] arr3 = { 1, 2, 3, 4, 5 };

Two-dimensional array initialization

//The first method of initialization: explicitly define the length of the array and then assign it individually
int[,] tar = new int[2, 2];
tar[0, 0] = 1;
tar[0, 1] = 1;
tar[1, 0] = 1;
tar[1, 1] = 1;
//The second method of initialization: explicitly define the length of the array and assign
int[,] tarr1 = new int[3, 2]
{
    { 1,1},
    { 2,2},
    { 3,3},
};

Initialization of three-dimensional array

//data body
int[,,] tharr = new int[3, 3, 3]
{
    //x0
    {
        //y,z
        { 1,2,3},
        { 4,5,6},
        { 7,8,9},
    },
    //x1
    {
        { 10,11,12},
        { 13,14,15},
        { 16,17,18},
    },
    //x2
    {
        { 19,20,21},
        { 22,23,24},
        { 25,26,27},
    },
};

(2). Get the length of the array

subscript = capacity - 1

1D array length

//Use the getlength() method to get the length of the array
int[] arr = new int[4];
int arr_length = arr.GetLength();

2D array length

using System;
namespace HelloWorldApplication
{
    class MyMain
    {
        static void Main(string[] args)
        {
            int[,] tarr1 = new int[3, 2]
            {
                { 1,1},
                { 2,2},
                { 3,3},
            };
            //capacity = row * width = [6]
            Console.WriteLine("The capacity of the array is:" + tarr1.Length);
            Console.WriteLine("The number of rows of the array is:" + tarr1.GetLength(0));
            Console.WriteLine("The number of columns in the array is:" + tarr1.GetLength(1));
            Console.WriteLine("the first of the array(1,1)The value above is:" + tarr1[0, 0]);
            Console.ReadKey();
        }
    }
}

output:

The capacity of the array is:6
 The number of rows of the array is:3
 The number of columns in the array is:2
 the first of the array(1,1)The value above is:1

3D array length

using System;
namespace HelloWorldApplication
{
    class MyMain
    {
        static void Main(string[] args)
        {
            //data body
            int[,,] tharr = new int[3, 3, 3]
            {
                 //x0
                {
                    //y,z
                    { 1,2,3},
                    { 4,5,6},
                    { 7,8,9},
                },
                //x1
                {
                    { 10,11,12},
                    { 13,14,15},
                    { 16,17,18},
                },
                //x2
                {
                    { 19,20,21},
                    { 22,23,24},
                    { 25,26,27},
                },
            };
            Console.WriteLine("the length of the three-dimensional array(capacity)for:" + tharr.Length);
            Console.WriteLine("three-dimensional array of x for:" + tharr.GetLength(0));
            Console.WriteLine("three-dimensional array of y for:" + tharr.GetLength(1));
            Console.WriteLine("three-dimensional array of z for:" + tharr.GetLength(2));
            Console.WriteLine("3D array(1,2,3)The value above is:" + tharr[0, 1, 2]);
            Console.ReadKey();
        }
    }
}

output:

the length of the three-dimensional array(capacity)for:27
 three-dimensional array of x for:3
 three-dimensional array of y for:3
 three-dimensional array of z for:3
 3D array(1,2,3)The value above is:6

(3). Operation data

Use the subscript to access the array memory to modify the operation data, and the subscript starts from 0.

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            int[] arr = new int[4];
            arr = new int[] { 1, 2, 3, 4 };
            for (int i = 0; i <= arr.Length - 1; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }
    }
}

output:

1
2
3
4

(4). Traverse

One-dimensional array traversal

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            //Array overflow compilation will not report an error, it will only report an error at runtime
            int[] arr2 = new int[5] { 1, 2, 3, 4, 5 };
            int num = 1;
            for (int i = 0; i <= arr2.Length - 1; i++)
            {
                Console.WriteLine("No." + num + "The value of an element is:" + arr2[i]);
                num++;
            }
            Console.ReadKey();
        }
    }
}

output:

The value of the first element is:1
 The value of the second element is:2
 The value of the third element is:3
 The value of the 4th element is:4
 The value of the fifth element is:5

2D array traversal

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            int[,] tarr1 = new int[3, 2]
            {
                {1,1},

                {2,2},

                {3,3},
            };
            for (int i = 0; i <= tarr1.GetLength(0) - 1; i++)
            {
                for (int j = 0; j <= tarr1.GetLength(1) - 1; j++)
                {
                    Console.WriteLine("No.[" + i + "," + j + "]The value of an element is:" + tarr1[i, j]);
                }
            }
            Console.ReadKey();
        }
    }
}

output:

No.[0,0]The value of an element is:1
 No.[0,1]The value of an element is:1
 No.[1,0]The value of an element is:2
 No.[1,1]The value of an element is:2
 No.[2,0]The value of an element is:3
 No.[2,1]The value of an element is:3

Three-dimensional array traversal

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            int[,,] tharr = new int[3, 3, 3]
            {
                //x0
                {
                    //y,z
                    { 1,2,3},
                    { 4,5,6},
                    { 7,8,9},
                },
                //x1
                {
                    { 10,11,12},
                    { 13,14,15},
                    { 16,17,18},
                },
                //x2
                {
                    { 19,20,21},
                    { 22,23,24},
                    { 25,26,27},
                },
            };
            for (int i = 0; i <= tharr.GetLength(0) - 1; i++)
            {
                for (int j = 0; j <= tharr.GetLength(1) - 1; j++)
                {
                    for (int k = 0; k <= tharr.GetLength(2) - 1; k++)
                    {
                        Console.WriteLine("No.[" + i + "," + j + "," + k + "]value is:" + tharr[i, j, k]);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

output:

No.[0,0,0]value is:1
 No.[0,0,1]value is:2
 No.[0,0,2]value is:3
 No.[0,1,0]value is:4
 No.[0,1,1]value is:5
 No.[0,1,2]value is:6
 No.[0,2,0]value is:7
 No.[0,2,1]value is:8
 No.[0,2,2]value is:9
 No.[1,0,0]value is:10
 No.[1,0,1]value is:11
 No.[1,0,2]value is:12
 No.[1,1,0]value is:13
 No.[1,1,1]value is:14
 No.[1,1,2]value is:15
 No.[1,2,0]value is:16
 No.[1,2,1]value is:17
 No.[1,2,2]value is:18
 No.[2,0,0]value is:19
 No.[2,0,1]value is:20
 No.[2,0,2]value is:21
 No.[2,1,0]value is:22
 No.[2,1,1]value is:23
 No.[2,1,2]value is:24
 No.[2,2,0]value is:25
 No.[2,2,1]value is:26
 No.[2,2,2]value is:27

2. Enumeration

A set of named integer constants, which themselves belong to value types, are used to increase data readability;

cannot inherit or pass on inheritance;

2.1 Statement

//Enums can be defined under a namespace (recommended) or under a class to use
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,
        summer,
        autumn,
        winter
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            Console.ReadKey();
        }
    }
}

output:

 

2.2 Instantiation

//Enumerations are defined under the namespace
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,
        summer,
        autumn,
        winter
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            Season a = Season.spring;
            Season b = Season.summer;
            Season c = Season.autumn;
            Season d = Season.winter;
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",a,b,c,d);
            Console.ReadKey();
        }
    }
}

output:

spring
summer
autumn
winter

2.3 Integers represented by enumerated lists

Each symbol in the enumerated list represents an integer value, an integer value greater than the symbol before it.

//By default, the value of the first enumeration symbol is 0;
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,//0
        summer,//1
        autumn,//2
        winter//3
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            int a = (int)Season.spring;
            int b = (int)Season.summer;
            int c = (int)Season.autumn;
            int d = (int)Season.winter;
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}\n{1}\n{2}\n{3}", Season.spring, Season.summer, Season.autumn, Season.winter);
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}\n{1}\n{2}\n{3}", a, b, c, d);
            Console.WriteLine("-------------------");
            Console.ReadKey();
        }
    }
}

output:

-------------------
spring
summer
autumn
winter
-------------------
0
1
2
3
-------------------

We can also change it manually, or assign a value to each list value, and the next value will be accumulated from the assigned place.

using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring = 10,//10
        summer,//11
        autumn = 9,//9
        winter//10
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            int a = (int)Season.spring;
            int b = (int)Season.summer;
            int c = (int)Season.autumn;
            int d = (int)Season.winter;
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}\n{1}\n{2}\n{3}", Season.spring, Season.summer, Season.autumn, Season.winter);
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}\n{1}\n{2}\n{3}", a, b, c, d);
            Console.WriteLine("-------------------");
            Console.ReadKey();
        }
    }
}

output:

-------------------
spring
summer
autumn
spring
-------------------
10
11
9
10
-------------------

2.4 Conversion of enumeration and int

(1) Enumeration is forced to int

//(int)
using System;
namespace HelloWorldApplication
{
	public enum Season
	{
		spring,
        summer,
        autumn,
        winter
	}
    class HelloWorld
    {
		static void Main()
        {
            //enum is forced to int
            Season i1 = Season.spring; 
            int num = (int)i1;
            Console.WriteLine(num);
            Console.ReadKey();
        }
    }
}

output:

0

(2) int strong conversion enumeration

//(Season)
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,
        summer,
        autumn,
        winter
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            //int strong to enum
            int num = 1;
            Season i1 = (Season)num;
            Console.WriteLine(i1);
            Console.ReadKey();
        }
    }
}

output:

summer

2.5 Conversion of enumeration and String

(1) Enumeration to String

//.ToString();
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,
        summer,
        autumn,
        winter
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            //enum is forced to string
            Season a = Season.summer;
            string b = a.ToString();
            Console.WriteLine(b);
            Console.ReadKey();
        }
    }
}

output:

summer

(2) String to enumeration

//(enumtype)Enum.Parse(typeof(enumtype),value)
using System;
namespace HelloWorldApplication
{
    public enum Season
    {
        spring,
        summer,
        autumn,
        winter
    }
    class HelloWorld
    {
        static void Main(string[] args)
        {
            //character in string
            string a_string = "spring";
            Season b_string = (Season)Enum.Parse(typeof(Season), a_string);
            Console.WriteLine(b_string);
            Console.WriteLine("--------------------");
            //The string is a number
            string a_int = "0";
            Season b_int = (Season)Enum.Parse(typeof(Season), a_int);
            Console.WriteLine(b_int);
            Console.ReadKey();
        }
    }
}

output:

spring
--------------------
spring

3. String

① Instead of an array of characters, it represents a string of characters.

② Declare a string variable with string;

③ string is an alias of the System.String class;

3.1 Method of creating String object

(1) Variable specification, + concatenation

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            string first_name = "Kawabata";
            string last_name = "Kang Cheng";
            string full_name = first_name + last_name;
            Console.WriteLine("full name is:{0}", full_name);
            Console.ReadKey();
        }
    }
}

output:

full name is:Kawabata Yasunari

(2) String class constructor

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            char[] word = { 'H', 'e', 'l', 'l', 'o' };
            String test = new String(word);
            Console.WriteLine(test);
            Console.ReadKey();
        }
    }
}

output:

Hello

(3) The method returns a string

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        static void Main()
        {
            string[] str_arr = { "Hello", "My", "name", "is", "TianshanGM", "." };
            string message = string.Join(" ", str_arr);
            Console.WriteLine(message);
            Console.ReadKey();

        }
    }
}

output:

Hello My name is TianshanGM .

(4) Format method conversion

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {
        public static void Main(String[] args)
        {
            DateTime now = new DateTime(2022, 12, 3, 20, 58, 30);
            string str_now = String.Format("time:{0:t}\n date:{0:D}", System.DateTime.Now);
            Console.WriteLine("Message:\n{0}",str_now);
            Console.ReadKey();
        }
    }
}

output:

Message:
time:23:09
 date:2023 March 6

3.2 Properties of the String class

(1) Chars

Finds the char object at the specified position within the specified pair object.

(2) Length

Get the number of characters in the current String object

3.3 String class methods

(1) Compare(String a,String b) [Static]

public static int Compare(string a,string b)
Compares the two specified string objects and returns an integer representing their relative position in the sort order. The method is case sensitive.
The return value is less than 0 a < b;
The return value is equal to 0 a = b;
The return value is greater than 0 a > b;

using System;
namespace HelloWorldApplication
{
    class HelloWorld
    {

        public static void Main(string[] args)
        {
            string a = "abcdesadasd";
            string b = "abcdeee";
            int result = string.Compare(a, b);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

output:

1

- March 6, 2023

Tags: C#

Posted by kraadde on Tue, 07 Mar 2023 22:57:34 +1030