Learning C #, started from Hello world

C # (pronounced "See Sharp") is an easy-to-use new programming language, not only object-oriented, but also the type of security. C # language derived from the C Series, C # can be used to build software components and applications that run on multiple operating systems (platform).

Because this series of articles will be widely used C # code sample, so we take a look at the way C # program, as well as its different parts what does it mean.

Manual carefully we start with a simple program to learn how to create and run a C # program.

Hello world

"Hello World" program has always used to introduce programming language. The following shows the program's C # code:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Create, edit, compile and run C # source code

Need to write C # code compiled and running, there are a number of different .NET framework for selection, this time to choose which .NET implementation (or .NET Framework) use. These implementations are usually packaged as a software development kit (Software Development Kit, SDK), the contents of multiple .NET Framework, please refer to this article: sfsd

Each installation process has the distinction of the .NET Framework. Recommended access https://www.microsoft.com/net/downloadfor specific download and installation instructions. First selected .NET Framework, and then select the packages to download depending on the target operating system.

Such as the .NET Framework sure what to use, it is selected by default .NET Core cross-platform capabilities. It can run on Linux, macOS and Microsoft Windows, so the priority use .NET Core.

There are many source code editing tools to choose from, including the most basic Windows Notepad, Mac / macOS TextEdit and Linux vi. But recommended to choose a somewhat simple and easy to grasp features powerful tools that should support at least a color label. Support of any C # code editor can be.

If you do not particularly like, open source recommend Studio Code Editor Visual ( https://code.visualstudio.com). If you are working on a Windows or Mac, you may also consider Microsoft Visual Studio 2017 (or later), the details of reference https://www.visualstudio.com.

Use .NET Core command-line interface (CLI) tool

.NET Core command-line interface (CLI) tools for cross-platform tool chain to develop new .NET applications. The following are the Windows to create, compile, and execute HelloWorld example program:

  1. To begin building .NET application, simply download and install the .NET SDK (Software Development Kit).

    Download .NET SDK (64-bit):https://download.visualstudio.microsoft.com/download/pr/8a382be6-d34e-4a15-92a1-e49fe2fa54d6/0038e8d5447470750098f8db5d836561/dotnet-sdk-3.0.100-win-gs-x64.exe

    Download .NET SDK (32-bit):https://download.visualstudio.microsoft.com/download/pr/8af457b6-8a58-4b61-a2b7-ee4fbeb0519b/190a2414988b3403cd66d4ef28dc9c55/dotnet-sdk-3.0.100-win-gs-x86.exe

  2. After the installation is complete, open a new command prompt and run the dotnetcommand. If the command is run, print out the information on how to use dotnet, the installation was successful.

    Here Insert Picture Description

  3. Navigate to the desktop.

    Here Insert Picture Description

  4. Run dotnet new console -o myAppcommand

    myApp

    This dotnetcommand creates a consoletype of application. -oCreate a file called myApp represents a folder, and put the code into the file directory.

    myApp folder Program.csfile is C # source code files. By default, it has the following contents:

    using System;
    
    namespace myApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
            }
        }
    }
  5. Run dotnet runcommand

    Here Insert Picture Description

    So far, it has been built and run on a .NET application!

  6. Any text editor can be used (e.g., Notepad) to open Program.csthe file, then the content of modified as follows:

    using System;
    
    namespace myApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                Console.WriteLine("The current time is " + DateTime.Now);
            }
        }
    }

    Save Program.csthe file, and then run again dotnet runcommand.

    Here Insert Picture Description

Using Visual Studio

Use Visual Studio 2017 to generate the Hello World application:https://docs.microsoft.com/zh-cn/dotnet/core/tutorials/with-visual-studio

Debugging Hello World application in Visual Studio 2017:https://docs.microsoft.com/zh-cn/dotnet/core/tutorials/debugging-with-visual-studio?tabs=csharp

使用 Visual Studio 2019 生成 Hello World 应用程序:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/inside-a-program/hello-world-your-first-program?tabs=windows

创建项目

无论 Dotnet CLI 还是 Visual Studio 都会自动创建几个文件。第一个是名为 Program.cs 的 C# 文件。虽然可选择任何名称,但一般都用 Program 这一名称作为控制台程序起点。.cs 是所有 C# 文件的标准扩展名,也是编译器默认要编译成最终程序的扩展名。

虽然并非一定需要,但通常都会为 C# 项目生成一个项目文件(.csproj 文件)。 项目文件的内容随不同应用程序类型和 .NET 框架而变。但至少会指出哪些文件要包含到编译中,要生成什么应用程序类型(控制台,Web,移动,测试项目等等),支持什么 .NET 框架,调试或启动应用程序需要什么设置,以及代码的其他依赖项(称为“库”)。

下面的代码列出了一个简单的 .NET Core 控制台应用项目文件。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

</Project>

编译和执行

dotnet build 命令生成名为 myApp.dll 的程序集(assembly)。扩展名 .dll 代表“动态链接库”(Dynamic Link Library, DLL)。 对于 .NET Core,所有程序集都使用 .dll 扩展名。在本例中,.NET Core 应用程序的编译输出默认放到子目录 ./bin/Debug/netcoreapp3.0/。之所以使用 Debug 这个名称,是因为默认配置就是 debug。该配置输出为调试而不是性能优化。

Here Insert Picture Description

编译好的输出本身不能执行(myApp.dll 不能双击运行)。相反,需用 CLI 来寄宿(host)代码。 对于 .NET Core 应用程序, 这要求 Dotnet.exe 进程作为应用程序的寄宿进程。

Here Insert Picture Description

Build executable applications using (generated after .NET Core 3.x .exefile), in the present embodiment, the ./bin/Debug/netcoreapp3.0/directory myApp.execan double-click operation.

to sum up

Benpian simply describes how to create a basic C # console application program. Way through this, you should have learned how to use Visual Studio to create and debug C # console application.

Guess you like

Origin www.cnblogs.com/vin-c/p/11990880.html