本文翻译自:What are the uses of “using” in C#

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. 用户kokos通过提及using关键字,回答了C#问题的精彩“ 隐藏功能” 。 Can you elaborate on that? 您能详细说明一下吗? What are the uses of using ? 使用有什么using


#1楼

参考:https://stackoom.com/question/Jc9/C-中-使用-的用途是什么


#2楼

public class ClassA:IDisposable{#region IDisposable Members        public void Dispose(){            GC.SuppressFinalize(this);}#endregion
}

public void fn_Data(){using (ClassA ObjectName = new ClassA()){//use objectName }}

#3楼

Using Clause is used to define the scope for the particular variable. 使用子句用于定义特定变量的范围。 For example: 例如:

     Using(SqlConnection conn=new SqlConnection(ConnectionString){Conn.Open()// Execute sql statements here.// You do not have to close the connection explicitly here as "USING" will close the connection once the object Conn becomes out of the defined scope.}

#4楼

using, in the sense of 在某种意义上使用

using (var foo = new Bar())
{Baz();
}

Is actually shorthand for a try/finally block. 实际上是try / finally块的简写。 It is equivalent to the code: 它等效于代码:

var foo = new Bar();
try
{Baz();
}
finally
{foo.Dispose();
}

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. 您当然会注意到,第一个代码段比第二个代码段要简洁得多,而且即使抛出异常,您也可能想做许多事情来进行清理。 Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. 因此,我们提出了一个称为Scope的类,该类使您可以在Dispose方法中执行任意代码。 So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this: 因此,例如,如果您有一个名为IsWorking的属性,在尝试执行某个操作后始终希望将其设置为false,则可以这样做:

using (new Scope(() => IsWorking = false))
{IsWorking = true;MundaneYetDangerousWork();
}

You can read more about our solution and how we derived it here . 您可以在此处阅读有关我们的解决方案以及如何派生更多信息的信息 。


#5楼

Everything outside the curly brackets is disposed, so it is great to dispose your objects if you are not using them. 大括号之外的所有内容都已处置,因此如果不使用它们,最好将其处置。 This is so because if you have a SqlDataAdapter object and you are using it only once in the application life cycle and you are filling just one dataset and you don't need it anymore, you can use the code: 之所以这样,是因为如果您有一个SqlDataAdapter对象,并且在应用程序生命周期中仅使用了一次,并且仅填充了一个数据集,而不再需要它,则可以使用以下代码:

using(SqlDataAdapter adapter_object = new SqlDataAdapter(sql_command_parameter))
{// do stuff
} // here adapter_object is disposed automatically

#6楼

You can make use of the alias namespace by way of the following example: 您可以通过以下示例使用别名名称空间:

using LegacyEntities = CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects;

This is called a using alias directive as as you can see, it can be used to hide long-winded references should you want to make it obvious in your code what you are referring to eg 如您所见,这被称为using别名指令 ,如果您想在代码中使其变得明显,则可以将其用于隐藏长引用,例如

LegacyEntities.Account

instead of 代替

CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects.Account

or simply 或简单地

Account   // It is not obvious this is a legacy entity

C#中“使用”的用途是什么相关推荐

  1. Python可以做什么?你所不知道的Python——生活中的奇妙用途

    大家都知道Python可以用来做数据分析.爬虫,甚至是人工智能,但却觉得那些东西比较遥远,会担心孩子学Python为时过早. Python 其实Python并不只是能做一些很专业.高端的应用,它在生活 ...

  2. 中key的用途_Micro Focus Operations Bridge Manager中的多个(RCE)漏洞

    从供应商的网站上. OBM作为操作桥为您的IT操作提供了一个单一的控制中心.所有来自服务器.网络.应用程序.存储和基础设施中其他IT孤岛的事件和性能管理数据都会被整合到一个先进的中央事件控制台的单一事 ...

  3. Java中finalize方法用途何在?

    1 package thinking.in.java.demo; 2 3 /* 4 * finalize的用途何在? 5 * 6 *本例的终止条件是L所有的Book对象在被当做垃圾回收前都应该被签入. ...

  4. 中key的用途_Python中的函数定义与参数使用

    本节知识点:(1)函数定义格式:(2)五种参数使用 函数定义 基本语法格式 def function_name( parameter_list ): return values 函数通常为小写英语单词 ...

  5. python函数中self的作用_在Python中self的用途是什么?

    如果您使用的是Python,那么"自我"一词是无法逃避的.它用于方法定义和变量初始化中.每次定义方法时,都会显式使用self方法.在本文中,我们将按以下顺序深入了解Python的自 ...

  6. java中 enum什么意思_Java中“enum”的用途是什么?

    本问题已经有最佳答案,请猛点这里访问. 所以我研究了这个"枚举"类型,在我看来它有点像一个美化的数组/ArrayList/List.它的具体用途是什么? 你觉得它是以什么样的方式排 ...

  7. python中else的作用_享学课堂谈python中else的用途

    今天我能聊聊python中的else,大家都知道 Python 中else的基本用法是在条件控制语句中的 if...elif...else...,但是else 还有两个其它的用途,一是用于循环的结尾, ...

  8. 算法在java的作用_了解InitialContext在java中的实际用途?

    我在遗留项目上工作最多,我发现这行ctx.lookup("datasource");很多次.根据我在Initial上下文中遇到的用法,它用于获取在webserver / appse ...

  9. 双足人形机器人在社会中的实际用途

    一般来说,双足机器人抓取过程中的四个关键任务:目标定位.姿态估计.抓取检测和运动规划.具体来说,目标定位包括目标检测和分割方法,无位姿估计的抓取检测.端到端抓取检测.端到端运动规划等.对这些方法进行了 ...

最新文章

  1. 凝聚406万开发者 飞桨十大发布提速产业智能化
  2. HTML的标签描述16
  3. img 光盘映像文件已损坏_系统封装||还在用MSDN下载Windows镜像文件?你out了,用这个就可以了...
  4. ProgressBar控件在Listview下的多线程应用(转自johngeng)
  5. SAP Fiori Elements 学习笔记 - 2021年4月19日
  6. 什么是 LOW-CODE ?
  7. 我最喜欢的Bash骇客
  8. 数据库并发抢红包_Redis悲观锁解决高并发抢红包的问题
  9. 电梯plc的io分配_用PLC构成液体混合控制系统IO分配及梯形图编程
  10. PS文字的投影怎么打?
  11. mac tab command没法切换窗口
  12. AlertManager警报通知 E-mail 微信 模板
  13. c++ protobuf中repeated类型使用——序列化
  14. 40岁想在职读计算机博士,年龄超过四十五岁还有机会报考在职博士吗
  15. 模拟直播间评论的动画
  16. 识别喜欢开发的程序员
  17. python while循环和for循环转换_Python的While循环和for循环,python,while
  18. 怎么画图自动生成HTML,用canvas写一个简易画图工具
  19. 个人计算机有ip地址吗,如何查看ip? 查看个人电脑IP地址五大方法
  20. HGame 2023 Week3 部分Writeup

热门文章

  1. 如何在Qt Creator中导入图标资源
  2. ACM学习历程—HDU5668 Circle(数论)
  3. ASP.NET之Application、Session和Cookie的差别
  4. POJ 2117 Electricity 割点 Tarjan算法
  5. 为什么只有软件就可以用盗版?
  6. zabbix 通过 SNMP 监控 Windows主机
  7. ELK下logstash收集java日志,多行合并成一行
  8. ELK分析tomcat的Catalina.out日志
  9. nginx启动报错 :./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object fi
  10. Win10- 日历 - 周日在第一列显示 - 设置方法