C#中using关键字的使用方法示例
作者:camellia 时间:2023-07-30 14:17:02
这里记录下C#中using关键字的使用方法。
Using的使用大致分别以下三种:
1 :using 指令(命名空间)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2 :using 语句
某些类型的非托管对象有数量限制或很耗费系统资源,在代码使用完它们后,尽可能快的释放它们时非常重要的。using语句有助于简化该过程并确保这些资源被适当的处置(dispose)。
这里大致分为两种形式。
(1 ):using (ResourceType Identifier = Expression ) Statement
Using表达式
圆括号中的代码分配资源,Statement是使用资源的代码
using语句会隐式产生处置该资源的代码,其步骤为:
a:分配资源
b:把Statement放进try块
c:创建资源的Dispose方法的调用,并把它放进finally块
// using 语句
using (usings.factory fac = new usings.factory())
{
fac.CreateProduct();
}
(2 ):using (Expression) Statement
Using对象
Expression 表示资源,Statement是使用资源,资源需要在using之前声明
usings.factory facs = new usings.factory();
// using 对象
using (facs)
{
facs.CreateProduct();
}
Factory类:
namespace usings
{
public class factory : IDisposable
{
public void CreateProduct()
{
Console.WriteLine("创建了一个产品!");
}
public void Dispose()
{
Console.WriteLine("该对象销毁~");
}
}
}
这里需要说我遇到的一个小问题,如图所示:
解决方法很简单,自定义的类继承IDisposable类,实现其Dispose方法。
具体如我上边的代码所示。
3 :using 别名
用using为命名空间或类型定义别名,当引入的多个命名空间包含相同名字的类型时,需要使用该类型时,可以通过using为其指定别名,使代码更加简洁,避免冲突。
这里放上这次测试使用的全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 别名
using afactory = usings.usingsaaa.factory;
using bfactory = usings.usingsbbb.factory;
namespace usings
{
class Program
{
static void Main(string[] args)
{
// using 语句
using (usings.factory fac = new usings.factory())
{
fac.CreateProduct();
}
Console.WriteLine("------------------------------------------");
usings.factory facs = new usings.factory();
// using 对象
using (facs)
{
facs.CreateProduct();
}
Console.WriteLine("------------------------------------------");
// 别名
afactory a = new afactory(); // 代替usingsaaa.factory
a.CreateProduct();
Console.WriteLine("------------------------------------------");
bfactory b = new bfactory(); // 代替usingsbbb.factory
b.CreateProduct();
Console.ReadKey();
}
}
namespace usingsaaa
{
public class factory
{
public void CreateProduct()
{
Console.WriteLine("aaa工厂创建了一个产品!");
}
}
}
namespace usingsbbb
{
public class factory
{
public void CreateProduct()
{
Console.WriteLine("bbb工厂创建了一个产品!");
}
}
}
namespace usings
{
public class factory : IDisposable
{
public void CreateProduct()
{
Console.WriteLine("创建了一个产品!");
}
public void Dispose()
{
Console.WriteLine("该对象销毁~");
}
}
}
}
来源:https://juejin.cn/post/7110889488628318245
标签:c#,using,关键字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android超清晰6.0权限申请AndPermission
2023-08-05 10:52:26
如何通过Java实现时间轴过程解析
2022-01-02 00:31:32
![](https://img.aspxhome.com/file/2023/8/67478_0s.png)
Java完美实现2048小游戏
2023-06-14 12:59:25
Android代码实现新年贺卡动画示例详解
2022-09-11 00:28:49
springboot自定义stater启动流程
2023-06-07 06:33:45
![](https://img.aspxhome.com/file/2023/5/65165_0s.png)
Android 中SP与DP的区别实例详解
2021-12-26 22:43:13
![](https://img.aspxhome.com/file/2023/7/138177_0s.jpg)
Maven打包没有指定主类问题(xxx.jar中没有主清单属性)
2023-11-26 21:34:44
Java源码解析之可重入锁ReentrantLock
2021-12-28 22:32:40
C#执行EXE文件与输出消息的提取操作
2023-02-28 00:08:06
![](https://img.aspxhome.com/file/2023/7/85717_0s.jpg)
Flutter利用Hero组件实现自定义路径效果的动画
2023-06-25 13:46:29
![](https://img.aspxhome.com/file/2023/3/85453_0s.png)
Android中RecyclerView实现分页滚动的方法详解
2023-11-13 13:59:53
![](https://img.aspxhome.com/file/2023/3/137673_0s.gif)
unity使用socket实现聊天室功能
2021-08-12 01:57:59
![](https://img.aspxhome.com/file/2023/5/108745_0s.jpg)
Android开发Compose集成高德地图实例
2022-09-05 01:44:28
![](https://img.aspxhome.com/file/2023/6/96766_0s.png)
Mybatis一对多查询的两种姿势(值得收藏)
2023-07-01 00:20:08
![](https://img.aspxhome.com/file/2023/9/61669_0s.png)
解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
2021-07-07 12:27:20
![](https://img.aspxhome.com/file/2023/9/71209_0s.jpg)
SpringBoot Pom文件依赖及Starter启动器详细介绍
2022-10-08 19:30:20
![](https://img.aspxhome.com/file/2023/7/80987_0s.png)
C#控制台进行文件读写的方法
2022-02-13 22:03:27
在spring boot3中使用native image的最新方法
2022-05-18 17:43:14
![](https://img.aspxhome.com/file/2023/5/89565_0s.png)
SpringBoot整合腾讯云COS对象存储实现文件上传的示例代码
2021-06-01 03:25:14
![](https://img.aspxhome.com/file/2023/0/113520_0s.jpg)
Spring Boot2发布调用REST服务实现方法
2023-12-10 20:03:49
![](https://img.aspxhome.com/file/2023/1/71141_0s.png)