C# using的几个用途

using 关键字有三个主要用途:

1.using 语句定义一个范围,在此范围的末尾将释放对象:

cs 复制代码
string filePath = "example.txt";
string textToWrite = "Hello, this is a test message!";
// Use the using statement to ensure the StreamWriter is properly disposed of
using (StreamWriter writer = new StreamWriter(filePath))
{
    writer.WriteLine(textToWrite);
}

2.using导入在其他命名空间中定义的类型:

cs 复制代码
using System;
using System.IO;

3.using 在命名空间下,为命名空间或类型或结构体创建别名

cs 复制代码
using Level = System.String;
using MyQuery = System.Linq;

~~~~~~

相关推荐
j7~10 分钟前
【C++ 八股文】(篇四)C++11 新特性
开发语言·c++·面试·c++11·八股文
eggwyw24 分钟前
C++中大括号的用法合集
开发语言·c++
xiaoye-duck26 分钟前
《C++ 项目:手写大模型接入 SDK》本地 Ollama 部署与适配
开发语言·网络·c++·语言模型
联犀1 小时前
联犀 Go SIMD 实战:视频转码从 16.5ms 到 5.46ms
开发语言·golang·音视频
octopus_c1 小时前
STL 之 string:auto 与范围 for、常用接口、模拟实现与深浅拷贝
开发语言·c++·算法
Nerd(BUG版)1 小时前
KMP学习day5--Kotlin Flow异步流基础
开发语言·学习·kotlin
寂寞的船1 小时前
【Unity Bug挑战】第10期 SerializeReference 内存泄漏之谜
unity·c#·游戏引擎·bug
CSharp精选营1 小时前
从控制台到「会动手」的 AI:用 Semantic Kernel 搭应用(小白教程)
c#·.net·ai agent·semantickernel·functioncalling
J.YA1 小时前
String字符串[面试题]
java·开发语言
xiaoye-duck12 小时前
《C++ 项目:手写大模型接入 SDK》Gemini接口设计(上):初始化、全量响应实现和测试
开发语言·网络·c++·语言模型