我的c#之路(3.using语句)
2014-04-25 21:26
411 查看
using语句
1.
-有助于确保正确处理IDisposable对象(如文件和字体)
-可以将多个对象与using语句一起使用,但必须在using语句中声明这些对象
-可以实例化资源对象,然后将变量传递给using语句,但这不是最佳做法。
1.
-有助于确保正确处理IDisposable对象(如文件和字体)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace c1 { class Program { static void Main() { using (System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\test.txt")) { string s = null; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } } }
-可以将多个对象与using语句一起使用,但必须在using语句中声明这些对象
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace c1 { class Program { static void Main() { using (Font font1 = new Font("Arial", 10.0f), font2 = new Font("Arial", 10.0f)) { //Use font1 and font2 } } } }
-可以实例化资源对象,然后将变量传递给using语句,但这不是最佳做法。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace c1 { class Program { static void Main() { Font font1 = new Font("Arial",10.0f); using (font1) { //Use font1 } //font1 is still in scope //but the method call throws an exception float f = font1.GetHeight(); } } }
相关文章推荐
- c#调用COM组件
- c#中虚函数的相关使用方法
- 浅析C# web访问mysql数据库-整理归纳总结
- C# IP地址与整数之间转换的具体方法
- C#中的多态深入理解
- C#实现的阴历阳历互相转化类实例
- C# 的关键字详细介绍
- c# 获得局域网主机列表实例
- C#基本概念列举详解
- C#中Convert.ToInt32()和int.Parse()的区别介绍
- C#面向对象特征的具体实现及作用详解
- c#静态方法和非静态方法详细介绍
- 关于C# Math 处理奇进偶不进的实现代码
- C#中调用Windows API的技术要点说明
- c# 随机函数的使用详解
- c#之圆形无标题栏椭圆窗体的实现详解
- C#页面之间跳转功能的小结
- 浅析C# 基础语法的使用
- C#中将字符串转换为整型的三种解决方法总结
- 深入c# GDI+简单绘图的具体操作步骤(一)