ASP.NET中using的用法三则

开发 后端
本文总结了asp.net中using 的三种用法:using指令,using语句以及using别名。

ASP.NET中using的用法1.using指令。using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到。

例如:using System;

    using System.Data;

ASP.NET中using的用法2.using语句,定义一个范围,在范围结束时处理对象。

场景:

当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。

要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。

例如:

public 
static DataTable GetTable(string sql, int executeTimeOut, string connStringName)  
        {  
            DataTable dtRet = new DataTable();  
            using (SqlConnection sc = new SqlConnection(connStringName))  
            {  
                using (SqlDataAdapter sqa = new SqlDataAdapter(sql, sc))  
                {  
                    sqa.SelectCommand.CommandTimeout = executeTimeOut;  
                    sqa.Fill(dtRet);                    
                    return dtRet;  
                }  
            }  
        }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

ASP.NET中using的用法3.using别名。using + 别名 = 包括详细命名空间信息的具体的类型。

这种做法有个好处就是当同一个cs引用了两个不同的命名空间,但两个命名空间都包括了一个相同名字的类型的时候。当需要用到这个类型的时候,就每个地方都要用详细命名空间的办法来区分这些相同名字的类型。而用别名的方法会更简洁,用到哪个类就给哪个类做别名声明就可以了。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的。

例如:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using aClass = ConsoleApplication1.MyClass;  
using bClass = ConsoleApplication2.MyClass;  
namespace ConsoleApplication1  
{  
    public 
class MyClass  
    {  
        public 
override 
string ToString()  
        {  
            return "You are in ConsoleApplication1.MyClass";  
        }  
    }  
    class TestUsing  
    {  
    }  
}  
namespace ConsoleApplication2  
{  
    class MyClass   
    {  
        public 
override 
string ToString()   
        {  
            return "You are in ConsoleApplication2.MyClass";  
        }  
    }  
}  
namespace TestUsing  
{  
    using ConsoleApplication1;  
    using ConsoleApplication2;  
    
    class ClassTestUsing  
    {        
        static 
void Main()  
        {              
              
            aClass my1 = new aClass();              
            Console.WriteLine(my1);  
            bClass my2 = new bClass();  
            Console.WriteLine(my2);  
            Console.WriteLine("ress any key");  
            Console.Read();  
        }  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

【编辑推荐】

  1. ASP.NET1.1验证码产生的原理及应用
  2. 静态文件处理:ASP.NET1.1和ASP.NET2.0对之不同
  3. 在ASP.NET中执行windows程序(ASP.NET1.1)
  4. 对asp.net1.1开发模板类的修改说明
  5. 用ASP.NET连接Oracle9i(ASP.NET1.1)
责任编辑:book05 来源: blog.sina
相关推荐

2009-08-17 17:36:57

ASP.NET缓存数据

2009-08-26 14:01:33

C# using用法

2009-07-24 11:24:33

ASP.NET中文乱码

2009-07-29 16:53:52

ASP.NET Get

2009-07-24 10:10:22

ASP.NET

2009-07-24 16:17:42

WebRequestEASP.NET

2009-07-30 13:07:49

ASP.NET中的三层

2009-07-22 18:08:00

ASP.NET基类

2009-09-07 18:53:46

static关键字

2009-07-30 13:28:55

ASP.NET中的ja

2009-07-20 13:32:24

ScriptManagASP.NET

2009-09-11 09:09:00

ASP.NETAdRotator控件

2009-07-31 10:08:33

OutputCache

2009-07-30 12:35:51

ASP.NET中的对象

2009-07-23 16:20:48

HTTP协议ASP.NET

2009-07-20 14:54:23

HttpWorkerRASP.NET

2009-07-20 15:19:52

ViewState本质ASP.NET

2009-07-22 17:55:52

2009-08-04 17:30:23

cookieless属ASP.NET

2009-08-04 18:05:37

动态编译ASP.NET
点赞
收藏

51CTO技术栈公众号