C # study notes

1, Variables and expressions

Use #region and #endregion to define the beginning and end of the code region that can be expanded and collapsed (any keyword starting with # is actually a preprocessing instruction, not strictly a C # keyword)

Integer types include:

typealiasAllowed values
sbyteSystem.SByte-128 ~ 127
byteSystem.Byte0 ~ 255
shortSystem.Int16-32768 ~ 32767
ushortSystem.UInt160 ~ 65535
intSystem.Int32-2147483648 ~ 2147483647
uintSystem.UInt320 ~ 4294967295
longSystem.Long64-9223372036854775808 ~ 9223372036854775807
ulongSystem.UInt640 ~ 18446744073709551615

Floating point types include:

  float,double,decimal

Text and Boolean include:

  char,string,bool

Use of simple type variables:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch03Ex01
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInteger;
            string myString;
            myInteger = 17;
            myString = "\"myInteger\" is";//When double quotation marks exist in the string itself, you need to use \ to represent escape characters to avoid errors
            Console.WriteLine($"{myInteger },{myString },{myInteger }");//$"{}" is also a way to insert a string. All literals in {} will be output
            Console.WriteLine(myString, myInteger);//In this way, you can also directly output the value of the variable
            Console.ReadKey();
        }
    }
}

Naming rules for variables:

Hump nomenclature: also known as small hump nomenclature, the first word is lowercase and the other words are capitalized.

  ex:
  int myAge;
  char myName[10];
  float manHeight;

String literal:

Escape sequenceGenerated characterUnicode value of the character
\'Single quotation mark0x0027
\''Double quotation mark0x0022
\\Backslash0x005C
\0null0x0000
\aWarning (beeping)0x0007
\bBackspace0x0008
\fPage change0x000C
\nLine feed0x000A
\renter0x000D
\tHorizontal tab0x0009
\vvertical tab 0x0008

The escape sequence can also be equivalent to: \ + U + four digit hexadecimal value. For example, \ u0027 is equivalent to a single quotation mark;

Word invariant specified string: @ character + string, for example, @ "Benjamin's string".

One word invariant strings are very useful in file names because they contain a large number of backslash characters:

  "C:\\Temp\\MyDir\\MyFile.doc"

  @"C:\Temp\MyDir\MyFile.doc"

A string is a reference type, while other types are numeric types. Therefore, a string can also be given a null value, indicating that the string variable does not reference a string or anything else.

2, Expression:

  1. Mathematical operator

Mainly including: + - * /%

When these operators are binary operators, they perform the corresponding addition, subtraction, multiplication and division functions. The results of the division of integer numbers are different from those of floating-point numbers; When + - is a unary operator, such as var1 =+ var2; It has no effect on the result and will not change any value. The most useful function of this operator is to customize relevant operations, which is mainly applied to operator overloading; var1 =- var2; Equivalent to var1 = var2 - 1;.

Note: when using mathematical operators for Boolean values, the compiler will report an error. When using mathematical operators for two character data, it is equivalent to converting two char variables into int, and then operating again. Finally, it is also an int (implicit conversion process). At the same time "+" can be used as a string connection operator to connect two strings.

  2. Increment decrement operator

It mainly includes: + +--

Using the increment decrement operator can simplify the expression form, but you need to pay attention to the position of operators and variables.

  3. Assignment Operators

It mainly includes: = + = - = * =/=

The assignment operator will bring the value to the left of the operator into the expression for calculation:

  var1 += var2;

Equivalent to:

  var1 = var1 + var2;

Like the "+" operator, "+ =" operator can also be applied to the connection between two strings.

  4. Priority of string:

"+ +" "--" of prefix and "+" "-" when used as unary operator

Priority over

Mathematical operator '*'% '/'

Above

  "+""-"

Above

Assignment operator

Above

"+ +" - "as suffix

3, Namespace

If the code in one namespace needs to use the name defined in another namespace, it must include a reference to the namespace and qualify the name to use the period character (.) between different namespace levels, For example:

  super.smashing.great

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch03Ex02
{
    class Program
    {
        static void Main(string[] args)
        {
            double firstNumber, secondNumber;
            string userName;
            Console.WriteLine("Enter your name:");
            userName = Console.ReadLine();//User input data
            Console.WriteLine($"Welcome {userName}!");
            Console.WriteLine("Now give me a number:");
            firstNumber = Convert.ToDouble(Console.ReadLine());//Convert the input data to double type, string to int: ToInt32
            Console.WriteLine("Now give me another number:");
            secondNumber = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine($"The sum of {firstNumber} and {secondNumber} is " +
                          $"{firstNumber + secondNumber}.");//The + sign here is a string operator, which can connect two strings
            Console.WriteLine($"The result of subtracting {secondNumber} from " +
                          $"{firstNumber} is {firstNumber - secondNumber}.");
            Console.WriteLine($"The product of {firstNumber} and {secondNumber} " +
                          $"is {firstNumber * secondNumber}.");
            Console.WriteLine($"The result of dividing {firstNumber} by " +
                          $"{secondNumber} is {firstNumber / secondNumber}.");
            Console.WriteLine($"The remainder after dividing {firstNumber} by " +
                          $"{secondNumber} is {firstNumber % secondNumber}.");
            Console.ReadKey();

        }
    }
}

Keywords: C#

Added by catalin.1975 on Fri, 21 Jan 2022 21:10:54 +0200