zoukankan      html  css  js  c++  java
  • using的几种用法 C#

    1. //using 用作命名空间指示符  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Text;  
    5. using System.IO;  
    6.   
    7. namespace statement  
    8. {  
    9.     //指定Font类的别名为F  
    10.     using F = System.Drawing.Font;  
    11.     class Program  
    12.     {      
    13.         static void Main(string[] args)  
    14.         {  
    15.             //using 语句管理资源的用法  
    16.             using (TextWriter W = File.CreateText("E://test.txt"))  
    17.             {  
    18.                 W.WriteLine(@"using 语句使用:using 语句允许程序员指定使用资源的对象应当何时释放资源。有资源管理的语句功能");  
    19.                 //使用别名来实例化对象  
    20.                 F font = new F("宋体",12);  
    21.                 W.WriteLine(font.Name.ToString() + font.Size.ToString());  
    22.             }  
    23.   
    24.             //上面的using语句等价于下面的预编译语句  
    25.             #if Debug  
    26.             TextWriter w = File.CreateText("E://test.txt");  
    27.             try  
    28.             {  
    29.                 w.WriteLine(@"using 语句使用:using 语句允许程序员指定使用资源的对象应当何时释放资源。有资源管理的语句功能");  
    30.             }  
    31.             finally  
    32.             {  
    33.                 //标准写法,下面语句也可以直接写成w.Dispose()  
    34.                 if(w != null)((IDisposable)w).Dispose();  
    35.             }  
    36.             #endif  
    37.   
    38.             //可以在using 语句中声明对象也可以在using 语句之前声明对象,如下:  
    39.             TextReader R = File.OpenText("E://test.txt");  
    40.             using (R)  
    41.             {  
    42.                 string Stringd = R.ReadToEnd();  
    43.                 Console.WriteLine(Stringd);  
    44.             }  
    45.         }  
    46.     }  
    47. }  
    //using 用作命名空间指示符 using System; using System.Collections.Generic; using System.Text; using System.IO;  namespace statement {     //指定Font类的别名为F     using F = System.Drawing.Font;     class Program     {             static void Main(string[] args)         {             //using 语句管理资源的用法             using (TextWriter W = File.CreateText("E://test.txt"))             {                 W.WriteLine(@"using 语句使用:using 语句允许程序员指定使用资源的对象应当何时释放资源。有资源管理的语句功能");                 //使用别名来实例化对象                 F font = new F("宋体",12);                 W.WriteLine(font.Name.ToString() + font.Size.ToString());             }              //上面的using语句等价于下面的预编译语句             #if Debug             TextWriter w = File.CreateText("E://test.txt");             try             {                 w.WriteLine(@"using 语句使用:using 语句允许程序员指定使用资源的对象应当何时释放资源。有资源管理的语句功能");             }             finally             {                 //标准写法,下面语句也可以直接写成w.Dispose()                 if(w != null)((IDisposable)w).Dispose();             }             #endif              //可以在using 语句中声明对象也可以在using 语句之前声明对象,如下:             TextReader R = File.OpenText("E://test.txt");             using (R)             {                 string Stringd = R.ReadToEnd();                 Console.WriteLine(Stringd);             }         }     } }

    我最常用的是:

    using (SqlConnection conn = new SqlConnection(SqlHelper.SqlConnection))
    {
        DataView dv = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0].DefaultView;
    }

  • 相关阅读:
    网络测量中基于Sketch方法的简单介绍
    Reading SBAR SDN flow-Based monitoring and Application Recognition
    Reading Meticulous Measurement of Control Packets in SDN
    Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing
    ovs加dpdk在日志中查看更多运行细节的方法
    后缀数组
    (转载)LCA问题的Tarjan算法
    Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
    Vijos 1816统计数字(计数排序)
    卡特兰数
  • 原文地址:https://www.cnblogs.com/top5/p/1586147.html
Copyright © 2011-2022 走看看