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;

~~~~~~

相关推荐
猫猫不是喵喵.2 分钟前
双亲委派机制与类加载过程
java·开发语言
布朗克16815 分钟前
Go入门到精通-22-同步原语
开发语言·后端·golang·同步原语
帅次28 分钟前
Kotlin Flow 与 StateFlow:UI 单向数据流基础
开发语言·ui·kotlin·stateflow·onlifecycle
艾伦野鸽ggg44 分钟前
JavaScript 浏览器本地存储
开发语言·javascript·ecmascript
czhc11400756631 小时前
719:StartPoint;Margin;ResourceDictionar;Component.model;lds;
c#
AOwhisky2 小时前
Python 学习笔记(第十一期)——运维自动化(上·后篇):进程级监控与子进程管理——psutil进阶
运维·开发语言·python·学习·云原生·运维开发
人工干智能2 小时前
Python 的链式调用:一连串的“点”调用
开发语言·python
就不掉头发2 小时前
如何优化程序的性能
开发语言
手写码匠2 小时前
MCP协议从零实现:手写一个完整的 Model Context Protocol 服务器与客户端
开发语言·数据结构
良木生香3 小时前
【C++初阶】STL—— Stack & Queue 从入门到精通:容器适配器、迭代器与经典面试题
java·开发语言·c++·算法·zookeeper