ASP.NET Tutorial/LINQ/Lambda

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Lambda expressions provide you with a terse way of defining methods.

File: NormalMethod.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Init()
    {
        btn.Click += new EventHandler(btn_Click);
    }
    void btn_Click(object sender, EventArgs e)
    {
        lblResult.Text = DateTime.Now.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Normal Method</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btn"
        Text="Go!"
        Runat="server" />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: AnonymousMethod.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Init()
    {
        btn.Click += delegate(object sender, EventArgs e){
            lblResult.Text = DateTime.Now.ToString();
        };
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Anonymous Method</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btn"
        Text="Go!"
        Runat="server" />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: LambdaExpression.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Init()
    {
        btn.Click += (sender, e) => lblResult.Text = DateTime.Now.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Lambda Expressions</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btn"
        Text="Go!"
        Runat="server" />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>