什么是.NET2.0中的新方法
上一篇 / 下一篇 2008-07-08 22:15:03 / 个人分类:转载
旧的方式。在ASP.NET1.0中。
1:stringstr ="Today is monday";
2:3:if(str.IndexOf("is")!= -1){
4://情况为True的操作
5:}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 stuff4:}
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:intiValue2: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: