C #- notes - from 0-1

Keywords: C# Back-end Visual Studio

Shortcut keys and Basics

Note:
//Single line note
/Multiline comment/
///Snippet comments

/Carriage return of r/n win system output
Ctrl+k+d quick alignment
Ctrl+k+c quick comment
Ctrl+k+u quick uncomment

@Function 1. Cancel the meaning drilling function of \ in the string
2. Output the string in the original format. Example:
Console.WriteLine(@"This is the first line.
This is the second line.");

Naming rules:

Camel: variable name - small hump
Pascal: class name - big hump

Prerequisite: type compatibility
Small to large: automatic
Big drill small: forced conversion, syntax: (type to be converted) value to be converted
If the two types are incompatible: you can convert using the convert conversion factory

String s = "123456";
Double d = Convert.ToDouble(s);//Cast to double type for operation
Double a = 12;
Double k = a + d;
Console.WriteLine("d={0},k={1}",d,a);//Placeholder stand in position before writing
Console.ReadKey();

How does the code work?

Console.WriteLine("Hello World!");

The Console section is called a class. The WriteLine() section is called the method.
• Console, Write, and Line initials
• use correct punctuation because they play a special role in C#
Write code in the. NET editor to display two messages
Console.WriteLine(@"This is the first line.
This is the second line.");

Accept console character conversion calculation
//Accept the user's score and output it

    Console.WriteLine("Please enter your name");
    String name = Console.ReadLine();//Accept user's fields
    Console.WriteLine("Chinese achievement");
    String strChinese = Console.ReadLine();
    Console.WriteLine("Mathematics achievement");
    String strMath = Console.ReadLine();
    Double  Chinese = Convert.ToDouble(strChinese);
    Double Math = Convert.ToDouble(strMath);
    Console.WriteLine("Name is{0}The total score is{1}The average score is{2:0.00}", name, Chinese + Math, (Chinese + Math)/2); //2:0.00 in the placeholder here means taking two decimal places  

+± - Operator

Int a , b = 2 ,c =3;
Int a = b++ * --c

//Output 4 b + + in which 2 participate in the operation -- c is 2 participate in the operation

Int a = ++b * c—
//When 9 ++b is output, 3 c is added before and 3 c is subtracted after
The judgment symbol | or & & and, and

/            //Leap year judgment
            //It can be divided by 400
            //It can be divided by 4 but not by 100
            Console.WriteLine("Please enter the year");
            int year = Convert.ToInt32(Console.ReadLine());
            Boolean b = year % 400 == 0 || year % 4 == 0 && year % 100 == 0;
            Console.WriteLine(b);
            Console.ReadKey();

In the figure above, it is obvious that & & has a higher priority than |, so it can't be seen. You can add () to make it more intuitive.

Exception capture:

Correct syntax, other problems at runtime
Use try catch to improve the robustness of the code. Write examples below.

Console.WriteLine("Please write a number");
            try
            {
                int b = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(b);
                Console.ReadKey();
            }
            catch {
                Console.WriteLine("There is a problem with the data entered");
                Console.ReadKey();
            }

The difference between Continue and break

Break statement, which can not only jump out of the "loop body", but also jump out of switch. But in fact, break can only be used in these two cases. The break statement cannot be used in any statement other than a loop statement and a switch statement.
The continue statement only ends this loop, not the entire loop. The break statement ends the whole loop process and no longer determines whether the condition for executing the loop is true. Moreover, continue can only be used in loop statements, that is, it can only be used in for, while and do... While. In addition, continue cannot be used in any statements.

Circulation

while loop
When the given condition is true, repeat the statement or statement group. It tests the condition before executing the loop body.
for/foreach loop
Execute a statement sequence multiple times to simplify the code for managing loop variables.
do... while loop
It is similar to the while statement except that it tests the condition at the end of the loop body.
Nested loop
You can use one or more loops within a while, for, or do... While loop.

Program debugging

Commissioning method:

  1. F11 sentence by sentence debugging (single step debugging)
  2. F10 process by process commissioning
  3. Breakpoint debugging

Ternary expression

Syntax: expression 1? Expression 2: expression 3;
Expression 1 is a relational expression
If the value of expression 1 is true, then the value of expression 2 is the value of the entire ternary expression
If the value of expression 1 is false, the value of expression 3 is the value of the entire ternary expression
Note: the result type of expression 2 must be consistent with the result type of expression 3 and the result type of the whole ternary expression.

    //Ternary expression
    Console.WriteLine("Please enter your name");
    String name = Console.ReadLine();
    String result = name == "Lao Zhao" ? "personnel" : "What carving";
    Console.WriteLine(result);
    Console.ReadKey();

Create random number

    //Create an object that produces random numbers
    Random r = new Random();
    //Let the object that generates the random number call the method to generate the random number
    for (int i = 0; i < 10; i++)
    {
        int rNumber = r.Next(1, 10);
        Console.WriteLine(rNumber);
    }
    Console.ReadKey();

Constant, enumeration, structure

Declaration syntax of constants:
//const variable type variable name = value;
            //const variable type variable name = value;
            const int num = 12;
            num = 21;
Error	1	The left-hand side of an assignment must be a variable, property or indexer	
Error 1 the left side of a task must be a variable, property, or indexer	
Constants cannot be changed;

Enumeration: enumeration standardizes our development, and stipulates that, for example, gender is not allowed to scribble (different words such as male, man, etc.)
Syntax:

[public] enum Enumeration name{Value 1, value 2...}

Generally, it is written under the namespace so that all classes can be used. Of course, it can also be written in the class, outside the main method; Enumeration can be interchanged with int type by default. Enumeration type and int type are compatible. All types can be converted to string type
example:

namespace test02
{
    public enum Gender
    {
        male,
        female
    }
    class Program
    {
        static void Main(string[] args)
        {
            
            Gender gerder = Gender.male;
            Console.WriteLine(gerder);
            Console.ReadKey();The lower bracket is not written

Structure: it can help us declare multiple variables of different types at one time. At the same time, it can be used with enumeration, for example, with the last name above;

[public] struct Structure name{Member variable}
It is placed in the namespace like enumeration
    public struct Person {
        public string _name;//It is generally underlined at the bottom to distinguish between fields and variables
        public int _age;
        public char _gender;//In addition, public can be called by the following methods, otherwise the permission is not enough
    }

Call in method

Person zsPerson;
            zsPerson.name = "Zhang San";
            zsPerson.age = 32;
            zsPerson.gender = 'male';
            Person lsPerson;
            lsPerson.name = "Li Si";
            lsPerson.age = 23;
            lsPerson.gender = 'female';
            Console.WriteLine(zsPerson.name);
            Console.WriteLine(lsPerson.name);

Array
Storing multiple variables of the same type at one time;
Syntax: array type [] array name = new array type [array length];
The length of the array cannot be changed. Pay attention to the bounds of the array
Calculate the maximum value and minimum value of the array

    int[] num = { 1, 2, 3, 4, 5, 54, 7, 8, 9, 45 };
    int max = 0;
    int min = 21;
    for (int i = 0; i < num.Length;i++ )
    {
        if(max < num[i]){
            max = num[i];
        }
        if(min > num[i]){
            min = num[i];
        }
    }
    Console.WriteLine(max);
    Console.WriteLine(min);
    Console.ReadKey();//In fact, it is still flawed, because there will be problems when Max and min are not necessarily assigned, so give them an initial value, which is the minimum, int max = int.MinValue; This value is the smallest in the int range. Similarly, vice versa: int min = int.MinValue;  

Posted by businessman332211 on Thu, 18 Nov 2021 19:49:52 -0800