开发环境搭建
参考官网
https://dotnet.microsoft.com/zh-cn/learn/dotnet/hello-world-tutorial/install
资源准备
下载 .NET SDK
https://dotnet.microsoft.com/zh-cn/download/dotnet
选择对应的版本下载,这里下载8.0版本的

查看对应的操作系统架构,选择对应的包下载
$ uname -a
SMP PREEMPT_DYNAMIC Sat Oct 31 06:52:47 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
这里选择对应的 x64 版本的
https://dotnet.microsoft.com/zh-cn/download/dotnet/thank-you/sdk-8.0.428-linux-x64-binaries-targz

安装 SDK
按照官方说明的步骤安装

我的操作系统是 Ubuntu 24.04 配置在 ~/.profile 目录了

配置完成之后执行 source 命令
$ source .profile
验证是否安装成功
终端中输入 dotnet
$ dotnet
Usage: dotnet [options]
Usage: dotnet [path-to-application]
Options:
-h|--help Display help.
--info Display .NET information.
--list-sdks Display the installed SDKs.
--list-runtimes Display the installed runtimes.
path-to-application:
The path to an application .dll file to execute.
查看命令行的安装位置
$ which dotnet
/home/scd/dotnet/dotnet
检查环境变量
$ echo $DOTNET_ROOT
/home/scd/dotnet
与Java 对比
基础信息
- Java 开发需要安装 Java SDK,C#开发需要安装 .NET SDK
- C 系语法与面向对象:代码块都使用花括{}、语句结尾用;,控制流(if/for)、类与接口的关键字几乎相同。两者都严格遵循封装、继承、多态等 OOP 范式。
- 强类型与跨平台:都是静态强类型语言,编译期会做类型安全检查。且现在都支持跨平台,能在 Windows、Linux、macOS 上运行。
- 自动 GC:都不需要手动 free 内存,分别依赖 JVM 和 .NET 运行时进行自动垃圾回收。
- 异常处理模型:都使用 try-catch-finally 结构来捕获和处理异常。
- Java 源码文件 .java,C#源文件 .cs
包管理工具对比
定位差异:谁自带包管理
C#:.NET SDK 内置 NuGet,dotnet add package 直接可用
Java:JDK 不含包管理,需另装 Maven 或 Gradle
本质:Java 把"构建+依赖"外置成独立工具,.NET 把它收进 SDK
命令行用法对比
C# 加包:dotnet add package Newtonsoft.Json
C# 还原:dotnet restore,依赖写在 .csproj
Java 加包:改 pom.xml 或 build.gradle 后跑 mvn dependency:resolve
Java 还原:mvn dependency:tree 查依赖树
配置文件与仓库
C#:.csproj 里写 ,源默认 nuget.org
Java:Maven 用 pom.xml,Gradle 用 build.gradle
共同点:都从中央仓库拉包,本地缓存后二次构建不重下
和 Maven、Gradle 的定位异同
相同:都是"声明式依赖+传递依赖解析+版本冲突处理"
不同:NuGet 与 SDK 强绑定,开箱即用;Maven/Gradle 可换、可定制仓库
粒度:Gradle 用 DSL 更灵活,Maven 用 XML 更约定化
hello world
创建项目
$ dotnet new console -n helloworld
创建完终端项目之后的文件结构
$ tree
.
└── hello-world
├── hello-world.csproj
├── obj
│ ├── hello-world.csproj.nuget.dgspec.json
│ ├── hello-world.csproj.nuget.g.props
│ ├── hello-world.csproj.nuget.g.targets
│ ├── project.assets.json
│ └── project.nuget.cache
└── Program.cs
3 directories, 7 files
查看 Program.cs 源码
$ cat Program.cs
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
编译项目
$ cd hello-world/
$ dotnet build
编译之后的生成文件
$ tree
.
├── bin
│ └── Debug
│ └── net8.0
│ ├── hello-world
│ ├── hello-world.deps.json
│ ├── hello-world.dll
│ ├── hello-world.pdb
│ └── hello-world.runtimeconfig.json
├── hello-world.csproj
├── obj
│ ├── Debug
│ │ └── net8.0
│ │ ├── apphost
│ │ ├── hello-world.AssemblyInfo.cs
│ │ ├── hello-world.AssemblyInfoInputs.cache
│ │ ├── hello-world.assets.cache
│ │ ├── hello-world.csproj.CoreCompileInputs.cache
│ │ ├── hello-world.csproj.FileListAbsolute.txt
│ │ ├── hello-world.dll
│ │ ├── hello-world.GeneratedMSBuildEditorConfig.editorconfig
│ │ ├── hello-world.genruntimeconfig.cache
│ │ ├── hello-world.GlobalUsings.g.cs
│ │ ├── hello-world.pdb
│ │ ├── ref
│ │ │ └── hello-world.dll
│ │ └── refint
│ │ └── hello-world.dll
│ ├── hello-world.csproj.nuget.dgspec.json
│ ├── hello-world.csproj.nuget.g.props
│ ├── hello-world.csproj.nuget.g.targets
│ ├── project.assets.json
│ └── project.nuget.cache
└── Program.cs
9 directories, 25 files
运行
$ ./bin/Debug/net8.0/hello-world
Hello, World!
本文面向Java开发者,详细指导如何在Linux(Ubuntu 24.04)环境下搭建C#开发环境,包括下载与安装.NET SDK 8.0、配置环境变量、验证安装,并系统对比Java与C#在基础特性(语法、OOP、GC、异常处理)、包管理机制(NuGet vs Maven/Gradle)及项目构建流程上的异同,突出.NET SDK内置包管理的开箱即用特性。

1506

被折叠的 条评论
为什么被折叠?



