C#基础二

.NET 平台知识讲解

在这里插入图片描述

设置字体
  1. 打开 Visual Studio 2022:
    首先启动 Visual Studio 2022。

  2. 进入选项菜单:
    点击顶部菜单栏中的 Tools(工具),然后选择 Options(选项)。

  3. 打开字体和颜色设置:
    在左侧栏中展开 Environment(环境),然后选择 Fonts and Colors(字体和颜色)。

  4. 选择字体类别:
    在右侧的 Show settings for:(显示设置)下拉菜单中,选择 Text Editor(文本编辑器)。这将允许你更改代码编辑器中的字体和颜色。

  5. 设置字体和大小:
    Font:(字体)下拉菜单中选择你喜欢的字体,然后在 Size:(大小)中设置字体大小。你可以立即在下方的预览框中查看效果。

  6. 应用并保存设置:
    点击 OK(确定)按钮应用并保存你的更改。

通过这些步骤,可以轻松地在 Visual Studio 2022 中自定义你的代码编辑器字体,使其更符合个人偏好,从而提高编程体验和效率。如果有其他设置需求,比如更改主题颜色或调整其他编辑器属性,可以在同一个 Options(选项)菜单中找到相应的选项进行设置。

二次编译
  1. 编写代码:
    编写的 C# 代码(源代码)会首先被编译成中间语言(IL)。

    Console.WriteLine("Hello, World!");
    
  2. 编译成 IL:
    使用 C# 编译器(csc.exe)将源代码编译成 IL。

  3. 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 Sub
End Module

通过 CLS 和 CTS 的支持,两个不同语言编写的代码可以无缝合作。

命名空间(Namespace)

命名空间是C#中用于组织代码的一种方式,避免命名冲突并提高代码的可读性和维护性。命名空间通过namespace关键字定义。

如何使用命名空间
  1. 定义命名空间:

    namespace MyApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
            }
        }
    }
    
  2. 使用命名空间:
    假设你在不同文件中定义了多个类,可以通过using关键字导入这些命名空间:

    using MyApplication.Utilities;
    
    class Program
    {
        static void Main(string[] args)
        {
            UtilityClass.DoSomething();
        }
    }
    
  3. 嵌套命名空间:
    可以在一个命名空间内嵌套其他命名空间以组织代码:

    namespace MyApplication
    {
        namespace Utilities
        {
            class UtilityClass
            {
                public static void DoSomething()
                {
                    Console.WriteLine("Doing something...");
                }
            }
        }
    }
    

Console 类的三种使用方法

  1. 读取输入:

    string userInput = Console.ReadLine();
    
  2. 输出信息:

    Console.WriteLine("Hello, World!");
    
  3. 格式化输出:

    int age = 25;
    Console.WriteLine("I am {0} years old.", age);
    

C# 的三种数值类型

  1. 整型(Integer):

    • int
    • long
    • short
  2. 浮点型(Floating Point):

    • float
    • double
  3. 十进制(Decimal):

    • decimal

C# 的三种非数值类型

  1. 字符型(Char):

    char letter = 'A';
    
  2. 字符串(String):

    string greeting = "Hello, World!";
    
  3. 布尔型(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 + 5
x -= 3;  // 相当于 x = x - 3
x *= 2;  // 相当于 x = x * 2
x /= 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 isGreaterOrEqual = (a >= b);// 大于等于
bool isLessOrEqual = (a <= b);   // 小于等于

字符串格式化

字符串格式化是将变量值插入字符串中的一种方法。C# 提供了多种格式化字符串的方法。

1 使用 + 号连接字符串
int age = 25;
string name = "Alice";
string info = "Name: " + name + ", Age: " + age;
Console.WriteLine(info);
2 使用字符串插值(插值字符串)
int age = 25;
string name = "Alice";
string info = $"Name: {name}, Age: {age}";
Console.WriteLine(info);
3 使用 String.Format 方法
int age = 25;
string name = "Alice";
string info = String.Format("Name: {0}, Age: {1}", name, age);
Console.WriteLine(info);

示例代码

以下是一个完整的示例代码,展示了变量的定义和使用、赋值运算符、算术运算符、比较运算符以及字符串格式化:

using System;

class Program
{
    static void Main()
    {
        // 变量定义和使用
        int age = 25;
        double salary = 50000.50;
        string name = "Alice";

        Console.WriteLine("Age: " + age);
        Console.WriteLine("Salary: " + salary);
        Console.WriteLine("Name: " + name);

        // 赋值运算符
        int x = 10;
        x += 5;
        x -= 3;
        x *= 2;
        x /= 2;

        Console.WriteLine("Value of x: " + x);

        // 算术运算符
        int a = 10;
        int b = 5;

        Console.WriteLine("Sum: " + (a + b));
        Console.WriteLine("Difference: " + (a - b));
        Console.WriteLine("Product: " + (a * b));
        Console.WriteLine("Quotient: " + (a / b));
        Console.WriteLine("Remainder: " + (a % b));

        // 比较运算符
        bool isEqual = (a == b);
        bool isNotEqual = (a != b);
        bool isGreater = (a > b);
        bool isLess = (a < b);
        bool isGreaterOrEqual = (a >= b);
        bool isLessOrEqual = (a <= b);

        Console.WriteLine("Is Equal: " + isEqual);
        Console.WriteLine("Is Not Equal: " + isNotEqual);
        Console.WriteLine("Is Greater: " + isGreater);
        Console.WriteLine("Is Less: " + isLess);
        Console.WriteLine("Is Greater Or Equal: " + isGreaterOrEqual);
        Console.WriteLine("Is Less Or Equal: " + isLessOrEqual);

        // 字符串格式化
        string info1 = "Name: " + name + ", Age: " + age;
        string info2 = $"Name: {name}, Age: {age}";
        string info3 = String.Format("Name: {0}, Age: {1}", name, age);

        Console.WriteLine(info1);
        Console.WriteLine(info2);
        Console.WriteLine(info3);
    }
}

该示例展示了如何在VS2022中使用C#编写代码,定义和使用变量,使用各种运算符以及进行字符串格式化。

  • 62
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 56
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 56
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ak2111

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值