vs code 寫C#心得


!!!官方指南請訪問:
https://docs.microsoft.com/zh-cn/dotnet/core/index
code runner 插件
————————————————————————————————————
還是用命令行運行來的簡便,不喜歡他的調試的配置,哭唧唧。
dotnet new console -n test
dotnet run
————————————————————————————————————
如果要調試,你得先把這個文件夾加入到工作空間:

然后,shift + command + p ,添加lauch.json tasks.json。

————————————————————————————————————
舉個例子:
'''
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"\({workspaceFolder}/ch4.csproj" ], "problemMatcher": "\)msCompile"
}
]
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "\({workspaceFolder}/bin/Debug/netcoreapp2.1/ch4.dll", "args": [], "cwd": "\){workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
'''
————————————————————————————————————
命令行方式:首先你得安裝相關的擴展,根據其要求還得安裝一個core
安裝之后,在vs code中打開terminal
使用命令創建工程 dotnet new console -n test
注意:加了-n 之后會創建一個文件夾名為test,不加則直接在當前目錄建工程
如果忘記了命令,可以 dotnet -h(還可以訪問官方文檔,地址在后面)
'''
dotnet -h
.NET Command Line Tools (2.1.500)

Usage: dotnet [sdk-options] [command] [command-options] [arguments]

sdk-options:
  -d|--diagnostics  Enable diagnostic output.
  -h|--help         Show command line help.
  --info            Display .NET Core information.
  --list-runtimes   Display the installed runtimes.
  --list-sdks       Display the installed SDKs.
  --version         Display .NET Core SDK version in use.

SDK commands:
  add               Add a package or reference to a .NET project.
  build             Build a .NET project.
  build-server      Interact with servers started by a build.
  clean             Clean build outputs of a .NET project.
  help              Show command line help.
  list              List project references of a .NET project.
  migrate           Migrate a project.json project to an MSBuild project.
  msbuild           Run Microsoft Build Engine (MSBuild) commands.
  new               Create a new .NET project or file.
  nuget             Provides additional NuGet commands.
  pack              Create a NuGet package.
  publish           Publish a .NET project for deployment.
  remove            Remove a package or reference from a .NET project.
  restore           Restore dependencies specified in a .NET project.
  run               Build and run a .NET project output.
  sln               Modify Visual Studio solution files.
  store             Store the specified assemblies in the runtime package store.
  test              Run unit tests using the test runner specified in a .NET project.
  tool              Install or manage tools that extend the .NET experience.
  vstest            Run Microsoft Test Engine (VSTest) commands.

Additional commands from bundled tools:
  dev-certs         Create and manage development certificates.
  ef                Entity Framework Core command-line tools.
  sql-cache         SQL Server cache command-line tools.
  user-secrets      Manage development user secrets.
  watch             Start a file watcher that runs a command when files change.

Run 'dotnet [command] --help' for more information on a command.

'''
使用命令運行工程 dotnet run
如果要用vs code進行調試:
按F5, 然后創建兩個文件:launch.json 和 tasks.json
注意要更改launch.json 中間的路徑名,例如:"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/test.dll"

Attention:
了解如何使用 dotnet-restore 命令還原依賴項和特定於項目的工具。
restore : 修復; 歸還; 交還; 使恢復
軟件開發工具包(縮寫:SDK、外語全稱:Software Development Kit)

官方指南請訪問:
https://docs.microsoft.com/zh-cn/dotnet/core/index

創建首個應用程序
安裝 .NET Core SDK 后,打開命令提示符。 鍵入以下 dotnet 命令以創建並運行 C# 應用程序。

dotnet new console
dotnet run
您應看到以下輸出:

Hello World!

摘要:
dotnet new
模板描述 模板名稱 語言
控制台應用程序 console [C#]、F#、VB
類庫 classlib [C#]、F#、VB
單元測試項目 mstest [C#]、F#、VB
xUnit 測試項目 xunit [C#]、F#、VB
Razor 頁 page [C#]
MVC ViewImports viewimports [C#]
MVC ViewStart viewstart [C#]
ASP.NET Core 空 web [C#],F#
ASP.NET Core Web 應用程序 (Model-View-Controller) mvc [C#],F#
ASP.NET Core Web 應用程序 razor, webapp [C#]
含 Angular 的 ASP.NET Core angular [C#]
含 React.js 的 ASP.NET Core react [C#]
含 React.js 和 Redux 的 ASP.NET Core reactredux [C#]
ASP.NET Core Web API webapi [C#],F#
Razor 類庫 razorclasslib [C#]
global.json 文件 globaljson
NuGet 配置 nugetconfig
Web 配置 webconfig
解決方案文件 sln

-h|--help
打印命令幫助。 可針對 dotnet new 命令本身或任何模板(如 dotnet new mvc --help)調用它。
若要了解如何創建自定義模板,請參閱 dotnet new 自定義模板。
-l|--list
列出包含指定名稱的模板。 如果針對 dotnet new 命令調用,則它列出可能對給定的目錄可用的模板。 例如,如果該目錄已包含一個項目,則它不會列出所有項目模板。
-lang|--language {C#|F#|VB}
要創建的模板的語言。 接受的語言因模板而異(請參閱參數部分中的默認值)。 對於某些模板無效。
備注
某些 shell 將 # 解釋為特殊字符。 在這些情況下,需要括住語言參數值,如 dotnet new console -lang "F#"。
-n|--name <OUTPUT_NAME>
所創建的輸出的名稱。 如果未指定名稱,使用的是當前目錄的名稱。
--nuget-source
指定在安裝期間要使用的 NuGet 源。
-o|--output <OUTPUT_DIRECTORY>
用於放置生成的輸出的位置。 默認為當前目錄。
--type
根據可用類型篩選模板。 預定義值為“project”、“item”或“other”。
-u|--uninstall <PATH|NUGET_ID>
從提供的 PATH 或 NUGET_ID 卸載源或模板包。
備注
若要使用 PATH 卸載模板,需要完全限定路徑。 例如,C:/Users/ /Documents/Templates/GarciaSoftware.ConsoleTemplate.CSharp 有效,但是包含文件夾中的 ./GarciaSoftware.ConsoleTemplate.CSharp 無效。 此外,模板路徑中不要包含最后的終止目錄斜杠。

dotnet run - 無需任何顯式編譯或啟動命令即可運行源代碼。
從 .NET Core 2.0 開始,無需運行 dotnet restore,因為它由需有還原的所有命令隱式運行,如 dotnet build 和 dotnet run。 在執行顯式還原有意義的某些情況下,例如 Azure DevOps Services 中的持續集成生成中,或在需要顯式控制還原發生時間的生成系統中,它仍然是有效的命令。
以長格式傳遞命令時,該命令也支持 dotnet restore 選項(例如,--source)。 不支持縮寫選項,例如 -s。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM