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 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏3 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
海盗12343 小时前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行4 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8284 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农5 小时前
功能测试--Day07--Python编程基础
开发语言·python
fengci.6 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
程序员zgh6 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
念何架构之路6 小时前
restartmanager-重启管理子系统
java·开发语言
SomeB1oody7 小时前
【RustyML入门】2.0. 经典机器学习
开发语言·后端·机器学习·rust·教程