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;

~~~~~~

相关推荐
Dxy12393102162 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
0566463 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄3 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰3 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#
哎呦喂我去去去3 小时前
C#实现屏幕墙:同时监控多个电脑桌面(支持Windows、信创Linux、银河麒麟、统信UOS)
linux·windows·c#
昆曲之源_娄江河畔3 小时前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql
Leighteen3 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
0566463 小时前
Python康复训练——控制流与函数
开发语言·python·学习
峥无3 小时前
C++11 深度详解:现代 C++ 基石全梳理
开发语言·c++·笔记
阿米亚波4 小时前
【C++ STL】std::unordered_multimap
开发语言·数据结构·c++·笔记·stl