作者:webabcd
介绍
C# 2.0 除了泛型之外的东东。
可空类型
-1
匿名方法
按钮被单击
按钮被单击(省略掉委托类型)
按钮被单击(不带参数的匿名方法)
按钮被单击(带参数的匿名方法)
迭代器
a
b
c
d
partial类
PartialTest
其它
insert
简体中文(GB2312)
OK
[源码下载]
介绍
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());
}
}
运行结果*可空类型
*/
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 />");
}
}
按了按钮后的运行结果* 匿名方法
*/
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 />");
}
}
}
运行结果* 迭代器
* 要实现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();
}
}
运行结果* 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; }
}
}
运行结果* 其它
*/
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
[源码下载]