当前位置:网站首页>[full stack plan - programming language C] basic introductory knowledge

[full stack plan - programming language C] basic introductory knowledge

2022-07-07 11:52:00 Poplar branch

Preface

Write a project oriented article for the first time , Whether it's Unity Game article , It is still used for services Unity Developed C# In the article , Will not be very patient to ponder the details .
 Insert picture description here
I don't want to pay attention to details , Just write something that can run , Digging deep is easy to consume your enthusiasm , And the articles written are also very verbose and cumbersome .

Get positive feedback when running , Don't keep groping for some details without a sense of achievement , This kind of self-confidence is easy to hit , Stride forward , You can do it without understanding , Then optimize , Constantly deal with details that have not been handled well 、bug, Iterative forward .

Hope everyone can enjoy the solution Bug The process of

 Insert picture description here

Understanding of editor

purple logo Our software is called Microsoft Visual Studio.
Blue logo Our software is called Visual Studio Code.
Both software are Microsoft's stuff , They are all software loved by developers , It's just that their fields are different .
 Insert picture description here
The differences between the two are roughly as follows , Just pass it by as common sense .
① violet logo The compiler , Blue logo Editor

VisualStudio( abbreviation VS) It's Microsoft's development kit series products , It's a basically complete set of development tools , It includes most of the tools needed throughout the software lifecycle , Such as UML Tools 、 Code control tools 、 Integrated development environment (IDE) etc. . Generally speaking , Is a ` compiler .

visual studio code Microsoft Corporation is a project : To run on Mac OS X、Windows and Linux Above , For writing modern Web And cross platform source code editor for Cloud Applications . informally , Is a Editor

② Cross platform operation capabilities are different .

Visual Studio Various functions can only be used in Windows and macOS(Mac OS X ) Run on , You cannot edit by skipping platforms .
visual studio code Is a true cross platform editor , It can be used on the platform that users are accustomed to , Instead of having to migrate to Windows On .

③ Function differently

Visual Studio It's the most popular at the moment Windows Integrated development environment for platform applications . Advanced development tools are provided 、 Debugging function 、 Database function and innovation function , Help quickly create the most advanced applications on various platforms , Developing new programs .
visual studio code It integrates all the features that a modern editor should have , Including syntax highlighting , Customizable hotkey binding , Bracket matching and code fragment collection , This editor also has the right Git Out of the box support .

 Insert picture description here

How to use it? C# Create project . And the hierarchical relationship of the whole project directory .

The process of creating a new project

① Create a new project on the homepage
 Insert picture description here

② The type selection is console application
 Insert picture description here
③ Configuration items

This is mainly to determine the name ( The name of the project and the name of the solution ) and Storage location of the project .
What I want to introduce here is project and Solution The relationship between . Projects are subsets of solutions , in other words , Multiple projects can be created under one solution , In a project , There is a main class with a main function and others that can declare several additional classes .

 Insert picture description here
Specific hierarchical relationship diagram
 Insert picture description here
④ Finally, pay attention to the menu bar and toolbar
 Insert picture description here

Go over the basics

One 、 Input and output

Want to run Hello World, Want to test A+B Result , Then it is inseparable from the use of the most basic output and input .
stay C# in , Want to input and output , Cannot leave use Console Class helps us meet our needs .
C# Console Class is mainly used for input and output operations of console applications
The following four methods are commonly used for input and output
 Insert picture description here
The format of the input statement is Console.ReadLine(); Pay attention to ,C# Input statement , The default input is string , If you want to input integer data, you need to convert .
Now I'll show you how to define a string to receive the input string information : string str = Console.ReadLine();
 Insert picture description here
If you want to input integer or floating-point data ,C# There are two ways of data conversion .

① Convert function

int num;
num = Convert.ToInt32(Console.ReadLine());

 because  Console.ReadLine()  Only accept data in string format .
 function  Convert.ToInt32()  Convert the data entered by the user into  int  data type 

 Insert picture description here
② Parse function

C# Parse  Method is used to convert a string type to any type , The specific grammatical form is as follows .
 data type     Variable name  =  data type .Parse( Value of string type );

 Insert picture description here

Two 、C# data type

stay C# in , Variables are divided into the following types :

 Value type (Value types)
 Reference type (Reference types)
 Pointer types (Pointer types)

① Value type

Value type variables can be assigned directly to a value . They are from class System.ValueType Derived from .
Value type Contains data directly .

② Reference type

The reference type does not contain the actual data stored in the variable , But they contain references to variables .
let me put it another way , They refer to a Memory location .

③ Pointer types

Pointer type variables store another type of memory address .C# Pointer in and C or C++ Pointers in have the same function .

Put it all together , It's this mind map
 Insert picture description here
If you want to get the exact size of a type or variable on a specific platform , have access to sizeof Operator . expression sizeof(type) Generates a storage size that stores objects or types in bytes

3、 ... and 、 Operator

Every language is similar , Just check and explain repeatedly , If you are not clear, you can check the document at any time to sweep away omissions
C# Operator

Four 、 Judgment statement 、 Loop statement

Judgment statements and loop statements are also similar to each statement
 Insert picture description here


 Insert picture description here

5、 ... and 、 Cognition of complex data types

① Constant

stay C# in , Can't use C and C++ In the usual way to make use #define Preprocessor instructions define constants .
Constants are used const Keywords to define ,const What is defined is static const .

static const Remember two points here :
① Load as class loads , namely , A class generation , It also generates .
② Can be called directly using classes . Its value is universal in the whole world . namely , I'm working on functions 1 It was modified in , So I'm in the function 4 When using this static constant in , It's not zero , Not other values , That's the function. 1 Modified results in .

② Enumeration type

Use enum To define enumeration types . In enumeration type , It can be understood as using from 0 The integer of the starting count refers to the data placed in it , such as 0 representative Spring 了 ,1 representative Summer 了 .

        //1、 Default writing 
        enum Season
        {
    
            Spring,
            Summer,
            Autumn,
            Winter
        }

        //2、 Custom type 
        enum ErrorCode : ushort
        {
    
            None = 0,
            Unknown = 1,
            ConnectionLost = 100,
            OutlierReading = 200
        }
        
		 // Enumeration type , It's from 0 At the beginning , that Season This enumeration has only 0,1,2,3
            var c = (Season)3;
            Console.WriteLine(c);// Output results :Winter

            // Test another number 4
            var d = (Season)4;
            Console.WriteLine(d);// Output results :4

③ Structure type

Use struct Keyword defines the structure type , Methods can be written in the structure

  public struct Coords
    {
    
        // Methods can be written in the structure 
        public Coords(double x,double y)
        {
    
            X = x;
            Y = y;
        }
        
        // Here is also the method , And it shows that there are references 
        public double X {
     get; }
        public double Y {
     get; }

        //C#  in  $  The function of symbols is C#6.0 A new feature in , That is, string splicing optimization .
        // ToString () Is to convert a non string value into a string 
        //=> yes Lambad expression :https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/lambda-operator#code-try-0
        // Toss it when writing an article 
        public override string ToString() => $"{X},{Y}";
    }

6、 ... and 、 Array

Declaration of arrays , and c and c++ Different .C and c++ It says double g[N][N]
java and C# It should be written


   double[] array1 = new double[5];


about C# for , Two dimensional arrays do need attention
Two dimensional arrays are different from conventional arrays

Declaration of two-dimensional array :
Type of element [ , ] Name of array =new Type of element [ Row number , Number of columns ];

int[,] arr = new int[4, 3]

One dimensional array ,Length Attribute represents the number of elements in the array , And in a two-dimensional array Length It means line * Column Result

a、 Use foreach It can be traversed simply and quickly .
b、 Array of GetLength() You can get the length of the specified latitude . Its method is passed into 0 You can get the number of lines . Pass in 1 You can get the number of columns

          int[,] arr = new int[,] {
     {
     1, 2 }, {
     3, 4 }, {
     5, 6 }, {
     7, 8 } };
          for (int i = 0; i < arr.GetLength(0); i++)
          {
    
              for (int j = 0; j < arr.GetLength(1); j++)
              {
    
                  Console.WriteLine (arr[i, j]);
              }
          }

summary

C# In the basic knowledge of , It's good to know what I listed above .
Most are similar to other languages , The sentence is if、while、for wait ;
Data types are int、double、long etc. .
I think the only thing worth noting is const The declared default is static constant and Declaration of two-dimensional array

The next one is to write a small one directly using basic knowledge Demo, Then proceed directly into C# Advanced features of

原网站

版权声明
本文为[Poplar branch]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070951200466.html

随机推荐