惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

博客园 - hulu

从一个int值显示相应枚举类型的名称或者描述 第8章 商品目录管理 第4章 创建商品目录 第2,第三章 概要 Scott Mitchell的ASP.NET2.0数据指南中文版索引 如何自定义gridview的表格显示? 7/14 原型编码阶段里程碑完成 asp.net2.0 的 gridview 7/9 原型编码阶段: (6) 使用DetailsView和FormView(7-5 11:54) - hulu 7/5 原型编码阶段: (5) 使用ADO.NET 7/3 原型编码阶段: (4) GridView的编辑更新 成功软件开发者的9种编程习惯(一) 6/29 原型编码阶段:(3) CommandField,TemplateField 6/29 原型编码阶段:(2) GridView的数据库操作 6/29 原型编码阶段:(1)连接并访问数据库 6/29 项目目录结构有所调整 6/28 项目原型前期实现任务确定 C# 2.0 泛型
C# 2.0 除了泛型
hulu · 2007-06-27 · via 博客园 - hulu

作者: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

[源码下载]