C# 2.0 除了泛型
生活随笔
收集整理的這篇文章主要介紹了
C# 2.0 除了泛型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[×××]
C# 2.0 除了泛型
作者:webabcd
介紹
C# 2.0 除了泛型之外的東東。
可空類型 /*
*可空類型????
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CSharp20_Nullable : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????// 針對值類型的可空類型
????????????????Nullable<DateTime> dt = null;
????????????????DateTime? dt2 = null;
????????????????// ??分配默認值
????????????????int? x = null;
????????????????int y = x ?? -1;
????????????????// x的默認值為0
????????????????Response.Write(y.ToString());
????????}
} 運行結果
-1
匿名方法 /*
* 匿名方法
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CSharp20_AnonymousMethod : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????Button btn = new Button();
????????????????btn.Text = "按鈕";
????????????????// 一般方法
????????????????btn.Click += new EventHandler(btn_Click);
????????????????// 省略掉委托類型
????????????????btn.Click += btn_Click2;
????????????????// 不帶參數的匿名方法
????????????????btn.Click += delegate { Response.Write("按鈕被單擊(不帶參數的匿名方法)<br />"); };
????????????????// 帶參數的匿名方法(參數數量和類型要一致)
????????????????btn.Click += delegate(object obj, EventArgs er) { Response.Write("按鈕被單擊(帶參數的匿名方法)<br />"); };
????????????????Page.Form.Controls.Add(btn);
????????}
????????void btn_Click(object sender, EventArgs e)
????????{
????????????????Response.Write("按鈕被單擊<br />");
????????}
????????void btn_Click2(object sender, EventArgs e)
????????{
????????????????Response.Write("按鈕被單擊(省略掉委托類型)<br />");
????????}
} 按了按鈕后的運行結果
按鈕被單擊
按鈕被單擊(省略掉委托類型)
按鈕被單擊(不帶參數的匿名方法)
按鈕被單擊(帶參數的匿名方法)
迭代器 /*
* 迭代器
* 要實現foreach必須實現接口IEnumerable或者IEnumerator
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class Charp20Yield
{
????????public static IEnumerable YieldTest()
????????{
????????????????string[] ary = new string[] { "a", "b", "c", "d", "e", "f", "g" };
????????????????foreach (string s in ary)
????????????????{
????????????????????????if (s != "e")
????????????????????????{
????????????????????????????????// 產生枚舉元素
????????????????????????????????yield return s;
????????????????????????}
????????????????????????else
????????????????????????{
????????????????????????????????// 中斷迭代
????????????????????????????????yield break;
????????????????????????}
????????????????}
????????}
}
public partial class CSharp20_Iterator : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????// 使用自定義迭代器
????????????????foreach (string s in Charp20Yield.YieldTest())
????????????????{
????????????????????????Response.Write(s);
????????????????????????Response.Write("<br />");
????????????????}
????????}
} 運行結果
a
b
c
d
partial類 /*
* partial類
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// partial類可以寫在不同的文件里,實際上就是取并集,類的修飾符要一致
public partial class PartialTest : IDisposable
{
????????public string Get()
????????{
????????????????return "PartialTest";
????????}
}
// partial類可以寫在不同的文件里,實際上就是取并集,類的修飾符要一致
public partial class PartialTest
{
????????public void Dispose()
????????{
????????????????// 略
????????}
}
public partial class CSharp20_Partial : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????PartialTest pt = new PartialTest();
????????????????Response.Write(pt.Get());
????????????????pt.Dispose();
????????}
} 運行結果
PartialTest
其它 /*
* 其它
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// 指定別名
using xyz = System.Text;
public partial class CSharp20_Other : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????CSharp20_Other_Test c = new CSharp20_Other_Test();
????????????????c.CurrentTime = DateTime.Now;
????????????????// 下面這句會報錯
????????????????// Response.Write(c.CurrentTime.ToLongDateString());
????????????????// 沒問題,因為set是internal的
????????????????c[2] = "insert";
????????????????Response.Write(c[2]);
????????????????Response.Write("<br />");
????????????????// 自定義類與framework類 類名重復時,自定義類優先
????????????????// xyz是命名空間別名
????????????????// 訪問全局的話用gloab
????????????????Response.Write(xyz::Encoding.Default.EncodingName);
????????}
}
public class CSharp20_Other_Test
{
????????private DateTime _currentTime;
????????public DateTime CurrentTime
????????{
????????????????// 給訪問器增加修飾符
????????????????// 如果接口定義訪問器了,則所有繼承的都是public
????????????????protected get { return _currentTime; }
????????????????set { _currentTime = value; }
????????}
????????private string[] ary = new string[] { "a", "b", "c", "d", "e", "f", "g" };
????????public string this[int index]
????????{
????????????????get { return ary[index]; }
????????????????// 給索引器增加修飾符
????????????????internal set { ary[index] = value; }
????????}
} 運行結果
insert
簡體中文(GB2312)
OK
[×××]
C# 2.0 除了泛型
作者:webabcd
介紹
C# 2.0 除了泛型之外的東東。
可空類型 /*
*可空類型????
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CSharp20_Nullable : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????// 針對值類型的可空類型
????????????????Nullable<DateTime> dt = null;
????????????????DateTime? dt2 = null;
????????????????// ??分配默認值
????????????????int? x = null;
????????????????int y = x ?? -1;
????????????????// x的默認值為0
????????????????Response.Write(y.ToString());
????????}
} 運行結果
-1
匿名方法 /*
* 匿名方法
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CSharp20_AnonymousMethod : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????Button btn = new Button();
????????????????btn.Text = "按鈕";
????????????????// 一般方法
????????????????btn.Click += new EventHandler(btn_Click);
????????????????// 省略掉委托類型
????????????????btn.Click += btn_Click2;
????????????????// 不帶參數的匿名方法
????????????????btn.Click += delegate { Response.Write("按鈕被單擊(不帶參數的匿名方法)<br />"); };
????????????????// 帶參數的匿名方法(參數數量和類型要一致)
????????????????btn.Click += delegate(object obj, EventArgs er) { Response.Write("按鈕被單擊(帶參數的匿名方法)<br />"); };
????????????????Page.Form.Controls.Add(btn);
????????}
????????void btn_Click(object sender, EventArgs e)
????????{
????????????????Response.Write("按鈕被單擊<br />");
????????}
????????void btn_Click2(object sender, EventArgs e)
????????{
????????????????Response.Write("按鈕被單擊(省略掉委托類型)<br />");
????????}
} 按了按鈕后的運行結果
按鈕被單擊
按鈕被單擊(省略掉委托類型)
按鈕被單擊(不帶參數的匿名方法)
按鈕被單擊(帶參數的匿名方法)
迭代器 /*
* 迭代器
* 要實現foreach必須實現接口IEnumerable或者IEnumerator
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class Charp20Yield
{
????????public static IEnumerable YieldTest()
????????{
????????????????string[] ary = new string[] { "a", "b", "c", "d", "e", "f", "g" };
????????????????foreach (string s in ary)
????????????????{
????????????????????????if (s != "e")
????????????????????????{
????????????????????????????????// 產生枚舉元素
????????????????????????????????yield return s;
????????????????????????}
????????????????????????else
????????????????????????{
????????????????????????????????// 中斷迭代
????????????????????????????????yield break;
????????????????????????}
????????????????}
????????}
}
public partial class CSharp20_Iterator : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????// 使用自定義迭代器
????????????????foreach (string s in Charp20Yield.YieldTest())
????????????????{
????????????????????????Response.Write(s);
????????????????????????Response.Write("<br />");
????????????????}
????????}
} 運行結果
a
b
c
d
partial類 /*
* partial類
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// partial類可以寫在不同的文件里,實際上就是取并集,類的修飾符要一致
public partial class PartialTest : IDisposable
{
????????public string Get()
????????{
????????????????return "PartialTest";
????????}
}
// partial類可以寫在不同的文件里,實際上就是取并集,類的修飾符要一致
public partial class PartialTest
{
????????public void Dispose()
????????{
????????????????// 略
????????}
}
public partial class CSharp20_Partial : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????PartialTest pt = new PartialTest();
????????????????Response.Write(pt.Get());
????????????????pt.Dispose();
????????}
} 運行結果
PartialTest
其它 /*
* 其它
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// 指定別名
using xyz = System.Text;
public partial class CSharp20_Other : System.Web.UI.Page
{
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????CSharp20_Other_Test c = new CSharp20_Other_Test();
????????????????c.CurrentTime = DateTime.Now;
????????????????// 下面這句會報錯
????????????????// Response.Write(c.CurrentTime.ToLongDateString());
????????????????// 沒問題,因為set是internal的
????????????????c[2] = "insert";
????????????????Response.Write(c[2]);
????????????????Response.Write("<br />");
????????????????// 自定義類與framework類 類名重復時,自定義類優先
????????????????// xyz是命名空間別名
????????????????// 訪問全局的話用gloab
????????????????Response.Write(xyz::Encoding.Default.EncodingName);
????????}
}
public class CSharp20_Other_Test
{
????????private DateTime _currentTime;
????????public DateTime CurrentTime
????????{
????????????????// 給訪問器增加修飾符
????????????????// 如果接口定義訪問器了,則所有繼承的都是public
????????????????protected get { return _currentTime; }
????????????????set { _currentTime = value; }
????????}
????????private string[] ary = new string[] { "a", "b", "c", "d", "e", "f", "g" };
????????public string this[int index]
????????{
????????????????get { return ary[index]; }
????????????????// 給索引器增加修飾符
????????????????internal set { ary[index] = value; }
????????}
} 運行結果
insert
簡體中文(GB2312)
OK
[×××]
轉載于:https://blog.51cto.com/webabcd/345400
總結
以上是生活随笔為你收集整理的C# 2.0 除了泛型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于SPECjAppServer评测,您
- 下一篇: Windows系统顽固型文件清除方法