C# example exercise 1: C# development environment and programming foundation

Personal notes, only for communication and learning!
If there are any mistakes, please criticize and point out!
Reference book: "C# Programming Experiment Guide and Exercise Test"

Notes are published on my personal blog synchronously, welcome to visit! portal



Purpose

  • Master the development of simple C# applications using the command line
  • Mastering writing console applications with VS
  • Understanding Application Command Line Arguments
  • Master VS debugging skills
  • Learn about VS's online help

Lab 1: Develop a Simple C# Application Using the Command Line

  • Open a text editor and add the following code.
using System;
namespace HelloWorldApplication
{
    
    
   class HelloWorld
   {
    
    
      static void Main(string[] args)
      {
    
    
         /* 我的第一个 C# 程序*/
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}
  • save file ashelloworld.cs

  • Open command prompt tool

  • Navigate to the directory where the file is saved.
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.9.1
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************

D:\Visual Studio 2019>j:

J:\>cd test

J:\test>
  • Type csc helloworld.csand press enterthe key to compile the code.
J:\test>csc helloworld.cs
Microsoft(R) Visual C# 编译器 版本 3.9.0-6.21124.20 (db94f4cc)
版权所有(C) Microsoft Corporation。保留所有权利。

J:\test>
  • If there are no errors in the code, the command prompt goes to the next line and the helloworld.exeexecutable .
  • Next, type helloworldto execute the program.
J:\test>helloworld
Hello World
  • You will see Hello Worldprinted on the screen.


If the system prompts that the csc command cannot be recognized, you need to configure environment variables. The configuration method is as follows.

Right-click on this computer and open Properties -> Advanced System Settings -> Environment Variables - Path> Add the following path under

C:\Windows\Microsoft.NET\Framework\v4.0.30319\

Note: v4.0.30319Yes .NET Framework, the latest version, you can view it under the path shown in the figure below


Experiment 2: Basic Use of VS

  • Start Visual Studio 2019 --> Create New Project

  • Select the corresponding template (C# for language, Windows for platform)

  • Choose console application

  • Give the project a name and choose a location to store it

  • Choose the appropriate .NETframe

  • The new project appears in Solution Explorer.

  • Write code in the code editor.
  • Run the program (ctrl+F5) (only run without debugging)


Lab 3: Familiarize yourself with command-line arguments in the Main method

using System;

namespace ConsoleApp2
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            if(args.Length==0)//如果命令行中的参数长度为0,也就是命令行参数为空
            {
    
    
                Console.WriteLine("请输入您的姓名做为参数!");
            }
            else//否则,也就是命令行中的参数不为空,则输出下面的语句
            {
    
    
                Console.WriteLine("您好!" + args[0]);
            }
            Console.ReadLine();//等待用户输入,作用是让程序停下来
        }
    }
}

The Main method is the main entry of the program, the parameters in it are a string array, and the command line parameters are added in the properties.

Right-click the project in "Solution Explorer" (the project in the screenshot is ConsoleApp2), select Debug in the pop-up dialog box, add the corresponding content in the application parameter input box in debugging and save it. After the program runs, the output is as follows


Experiment 4: Trace debugging of programs in VS environment

Program errors are often called bugs, and the process of debugging is called debug. Common types of errors in programs are as follows

1. Compilation error

using System;

namespace ConsoleApp2
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int a = 20
            int b = 5
            int c = 100 / a + b
            Console.WriteLine(c)
            Console.ReadLine()
        }
    }
}

Of course, this error is obvious, but it is also very common, and this kind of error is easy to solve according to the error message of the compiler!

namespace ConsoleApp2
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int a = 20;
            int b = 5;
            int c = 100 / a + b;
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }
}

When using the corresponding method, if the namespace is not referenced, the following error message will appear!

2. Runtime error

The most common runtime error is the "division by zero" error, assigning the integer variable a in the above code to 0; because 0 cannot be used as a divisor, the program will have an error at runtime!

using System;

namespace ConsoleApp2
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int a = 0;
            int b = 5;
            int c = 100 / a + b;
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }
}

3. Logical errors

using System;

namespace ConsoleApp2
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int n, n2, n3;
            n = 5;
            n2 = n * n;
            n3 = n2 * n2;//本该是n*n2
            Console.WriteLine("{0},{1},{2}", n, n2, n3);
            Console.ReadLine();
        }
    }
}

In the above program, although there are no compilation errors and runtime errors, there is a problem with the logic in the program, which makes us unable to obtain the results we want through this program. Such errors are difficult to solve. When writing the program Special attention is required!

4. The most frequently used shortcut keys in the debugging process

F5

Start debugging, often used to go directly to the next breakpoint.

F9

The important role of creating and canceling breakpoints Breakpoints can be set anywhere in the program. In this way, the program can be stopped at the desired position at will, and then executed step by step.

F10

Procedure by procedure, usually used to deal with a procedure, a procedure can be a function call, or a statement.

F11

Statement-by-statement is to execute a statement each time, but this shortcut key can make our execution logic enter the function (this is the longest).

CTRL + F5

Start execution without debugging, if you want the program to run directly without debugging, you can use it directly.


Experiment 5: VS online help function

Guess you like

Origin blog.csdn.net/weixin_50915462/article/details/114958121