什么是.NET2.0中的新方法

上一篇 / 下一篇  2008-07-08 22:15:03 / 个人分类:转载

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }1. String.Contains()
    旧的方式。在ASP.NET1.0中。
1:stringstr ="Today is monday";
2:
3:if(str.IndexOf("is")!= -1){
4://情况为True的操作
5:}
  新的途径。在ASP.NET2.0中。
1:stringstr ="Today is monday";
2:if(str.Contains("is")) {
3:// 情况为True的操作
4:}
 
2.String.IsNullOrEmpty()
    旧的方式。在ASP.NET1.0中。

1:stringstr ="Alvin Chooi";


2:if((str!=null) && (str.Length > 0)) {
3:// 情况为True的操作


4:}
  新的途径。在ASP.NET2.0中。

1:stringstr ="Alvin Chooi";




2:if(!String.IsNullOrEmpty(str)) {
3:// do stuff


4:}



3.TryParse()
  旧的方式。在ASP.NET1.0中。


1:intiValue;


2:try{

3:iValue = Int32.Parse("2x");

4:returntrue;

5:}

6:catch(Exception ex) {

7:returnfalse;

8:}


新的途径。在ASP.NET2.0中。


1:intiValue

2:boolisValid = Int32.TryParse("2x",outiValue);




4.Non-null operator (??)操作符。。
  旧的方式。在ASP.NET1.0中。

    string str = null;
   if(str!=null)
        Response.Write(str);
    else
        Response.Write("It is null");

 新的途径。在ASP.NET2.0中。
    Response.Write(str?? "It is null");


5.Eval() & Bind() in ASP.NET 2.0
  旧的方式。在ASP.NET1.0中。


    <%# DataBinder.Eval(Container.DataItem,"MyField") %>

 新的途径。在ASP.NET2.0中。
    <%# Eval("MyField") %> // 第一种绑定

    <%# Bind("MyField") %> // 第二种绑定

6.String.StartsWith()
  旧的方式。在ASP.NET1.0中。


    string str = "Alvin Chooi";
    if(str.IndexOf("Al")==0) {
        // 情况为True的时候做事
    }

 新的途径。在ASP.NET2.0中。    
    string str = "Alvin Chooi";
    if(str.StartsWith("Al")) {
        // 情况为True的时候做事
    }

7. String.EndsWith()
  旧的方式。在ASP.NET1.0中。

    string str = "Alvin Chooi";

    if(str.IndexOf("Al")== str.Length-2) {
        // 情况为True的时候要做的事。

    }

 新的途径。在ASP.NET2.0中。    
    string str = "Alvin Chooi";
    if(str.EndsWith("Al")) {
        // 情况为True的时候要做的事。
    }

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar