C # development environment and fundamentals of programming

Posted by morph07 on Tue, 18 Jan 2022 13:07:37 +0100

Experiment 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)
      {
         /* My first C# program*/
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}
  • Save the file as HelloWorld cs
  • Open the 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 CS and press enter to compile the code.
csc helloworld.cs
Microsoft(R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
copyright(C) Microsoft Corporation. All rights reserved.

J:\test>
  • If there are no errors in the code, the command prompt goes to the next line and generates HelloWorld Exe executable.
  • Next, type Hello world to execute the program.
J:\test>helloworld
Hello World
  • You will see Hello World printed on the screen.

If the system prompts that the csc command cannot be recognized, the environment variable needs to be configured as follows.

Right click this computer to open properties - > advanced system settings - > environment variables - > add the following Path under Path

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

Note: v4 0.30319 yes NET Framework. You can view it in the following path

Experiment 2: basic use of Visual Studio 2019

  • Start Visual Studio 2019 - > create a new project
  • Select the corresponding template (language C#, platform Windows)
  • Select console application
  • Give your project a name and choose where to store it
  • Select the appropriate NET framework
  • The new project appears in solution explorer.
  • Write code in the code editor.
  • Run the program (ctrl+F5) (only run without debugging)

Experiment 3: be familiar with the command line parameters in the Main method

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length==0)//If the length of the parameter in the command line is 0, the command line parameter is empty
            {
                Console.WriteLine("Please enter your name as the parameter!");
            }
            else//Otherwise, if the parameter in the command line is not empty, the following statement will be output
            {
                Console.WriteLine("Hello!!" + args[0]);
            }
            Console.ReadLine();//Wait for user input to stop the program
        }
    }
}

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) and select debugging in the pop-up dialog box. Add corresponding content in the application parameter input box in debugging and save it. After the program runs, the output is as follows

Experiment 4: program tracking and debugging

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

Compile type 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. This kind of error is easy to solve according to the error information 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 its namespace is not referenced, the following error message will appear!

Runtime error

The most common runtime error is the "divide by zero" error, such as assigning the integer variable a in the above code to 0; There is no syntax error in the program itself, but because 0 cannot be a multiplier, the program will have a runtime error!

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();
        }
    }
}

Logical error

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, n2, n3 = 0;
            n = 5;
            n2 = n * n;
            n3 = n2 * n2;//It should have been 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 leads us to obtain the results we want through this program. Such errors are the most difficult to find and need special attention when writing code!

Several shortcut keys most commonly used in debugging

F5

Start debugging is often used to directly adjust to the next breakpoint.

F9

The important role of creating and canceling breakpoints is to set breakpoints anywhere in the program. In this way, the program can stop executing at the desired position, and then execute step by step.

F10

Procedure by procedure is usually used to process a procedure. A procedure can be a function call or a statement.

F11

Statement by statement is to execute one statement at a 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: Visual Studio 2019 online help function