行业知识
C#基础二
.NET 平台知识讲解
设置字体
-
打开 Visual Studio 2022:
首先启动 Visual Studio 2022。
-
进入选项菜单:
点击顶部菜单栏中的 Tools(工具),然后选择 Options(选项)。
-
打开字体和颜色设置:
在左侧栏中展开 Environment(环境),然后选择 Fonts and Colors(字体和颜色)。
-
选择字体类别:
在右侧的 Show settings for:(显示设置)下拉菜单中,选择 Text Editor(文本编辑器)。这将允许你更改代码编辑器中的字体和颜色。
-
设置字体和大小:
在 Font:(字体)下拉菜单中选择你喜欢的字体,然后在 Size:(大小)中设置字体大小。你可以立即在下方的预览框中查看效果。
-
应用并保存设置:
点击 OK(确定)按钮应用并保存你的更改。
通过这些步骤,可以轻松地在 Visual Studio 2022 中自定义你的代码编辑器字体,使其更符合个人偏好,从而提高编程体验和效率。如果有其他设置需求,比如更改主题颜色或调整其他编辑器属性,可以在同一个 Options(选项)菜单中找到相应的选项进行设置。
二次编译
-
编写代码:
编写的 C# 代码(源代码)会首先被编译成中间语言(IL)。
Console.WriteLine("Hello, World!"); -
编译成 IL:
使用 C# 编译器(csc.exe)将源代码编译成 IL。
-
JIT 编译:
程序运行时,CLR(公共语言运行时)将 IL 编译成本机代码执行。
托管代码和非托管代码
-
托管代码:
运行在 CLR 上的代码。由 .NET 运行时管理内存、垃圾回收等。
int x = 10;
-
非托管代码:
直接运行在操作系统上,不受 CLR 管理。比如使用 PInvoke 调用 Windows API。
[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);CLR 虚拟机用途
CLR(Common Language Runtime)是 .NET 框架的运行时环境,提供以下功能:
- 内存管理
- 线程管理
- 异常处理
- 安全性
- 垃圾回收
CLS 和 CTS 在混合编程中的作用与测试
-
CLS(Common Language Specification):
定义了一组语言互操作的规则,确保不同语言之间的代码可以互操作。
// C# 代码public class Example{ public int Add(int a, int b) => a + b;}' VB.NET 代码Dim example As New Example()Dim result As Integer = example.Add(5, 10)
-
CTS(Common Type System):
规定了 .NET 中的数据类型,确保不同语言定义的类型可以互操作。
// C# 代码int number = 123;
' VB.NET 代码Dim number As Integer = 123
示例
假设我们用 C# 编写一个库,并在 VB.NET 中使用:
// C# 代码public class Calculator{ public int Add(int a, int b) { return a + b; }}' VB.NET 代码Module Module1 Sub Main() Dim calc As New Calculator() Dim result As Integer = calc.Add(5, 10) Console.WriteLine("Result: " & result) End SubEnd Module通过 CLS 和 CTS 的支持,两个不同语言编写的代码可以无缝合作。
命名空间(Namespace)
命名空间是C#中用于组织代码的一种方式,避免命名冲突并提高代码的可读性和维护性。命名空间通过namespace关键字定义。
如何使用命名空间
-
定义命名空间:
namespace MyApplication{ class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }} -
使用命名空间:
假设你在不同文件中定义了多个类,可以通过using关键字导入这些命名空间:
using MyApplication.Utilities;class Program{ static void Main(string[] args) { UtilityClass.DoSomething(); }} -
嵌套命名空间:
可以在一个命名空间内嵌套其他命名空间以组织代码:
namespace MyApplication{ namespace Utilities { class UtilityClass { public static void DoSomething() { Console.WriteLine("Doing something..."); } } }}
Console 类的三种使用方法
-
读取输入:
string userInput = Console.ReadLine();
-
输出信息:
Console.WriteLine("Hello, World!"); -
格式化输出:
int age = 25;Console.WriteLine("I am {0} years old.", age);
C# 的三种数值类型
-
整型(Integer):
- int
- long
- short
-
浮点型(Floating Point):
- float
- double
-
十进制(Decimal):
- decimal
C# 的三种非数值类型
-
字符型(Char):
char letter = 'A';
-
字符串(String):
string greeting = "Hello, World!";
-
布尔型(Boolean):
bool isTrue = true;
变量定义和使用
在C#中,变量是存储数据的命名位置。定义变量需要指定类型和名称,然后可以赋值并使用。
// 定义变量int age; // 整数类型变量double salary; // 浮点数类型变量string name; // 字符串类型变量// 变量赋值age = 25;salary = 50000.50;name = "Alice";// 使用变量Console.WriteLine("Age: " + age);Console.WriteLine("Salary: " + salary);Console.WriteLine("Name: " + name);赋值运算符
赋值运算符用于给变量赋值。最常用的是 = 运算符。
int x;x = 10; // 赋值运算符x += 5; // 相当于 x = x + 5x -= 3; // 相当于 x = x - 3x *= 2; // 相当于 x = x * 2x /= 2; // 相当于 x = x / 2
算术运算符
算术运算符用于执行数学运算。
int a = 10;int b = 5;int sum = a + b; // 加法int difference = a - b; // 减法int product = a * b; // 乘法int quotient = a / b; // 除法int remainder = a % b; // 取余
比较运算符
比较运算符用于比较两个值,并返回一个布尔值。
int a = 10;int b = 5;bool isEqual = (a == b); // 等于bool isNotEqual = (a != b); // 不等于bool isGreater = (a > b); // 大于bool isLess = (a = b);// 大于等于bool isLessOrEqual = (a
-
-