LINQ 为C#开发的一种类似于SQL的语言

MSXML和ADO.NET使我们能够遍历和操作XML文档,但它们都无法让开发人员在编写数据访问操作代码时觉得舒服、自然。

LINQ使数据访问成为了.NET中的一个高级编程概念,它使得开发人员能够完全依靠智能感知技术来创建类型安全的数据访问代码编译期的语法检查

using语句是try...finally的简洁表示。当所创建的类型实现了IDisposable时,则可以直接使用using。

匿名方法(anonymous method)

// Create a handler for a click event
button1.Click += delegate(System.Object o, System.EventArgs e)
{ System.Windows.Forms.MessageBox.Show("Click!"); };

对象、集合初始化器语法,编译器会将集合初始化代码转换成完整的形式,节省出大量的编码时间。P28 《LINQ编程技术内幕》

Chapter 3: 扩展方法(Extension Method): 允许扩展密封类和内部类型,它还能避免深度继承树,它提供了一种无需继承的添加行为的方式。

示例演示了如何添加一个名为Dump的扩展方法,第一个参数类型之前必需使用this修饰符。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var song = new { Artist = "JUssi", Song = "Aida" };
            song.Dump();
            Console.Read();
        }
    }

    public static class Dumper
    {
        public static void Dump(this Object o) // 作用于object类型,而所有类型又都继承自object.
        {
            PropertyInfo[] properties = o.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                try
                {
                    Debug.WriteLine(string.Format("Name: {0}, Value: {1}", p.Name,
                        p.GetValue(o, null)));
                }
                catch
                {
                    Debug.WriteLine(string.Format("Name: {0}, Value: {1}", p.Name,
                        "unk."));
                }
            }
        }

        public static void Dump(this IList list)

        {

             foreach(object o in list)

                  o.Dump();

        }

 }
}

P60~64示例3-6和3-8对比,说明Linq使得代码更加简洁。Linq的基础就是扩展方法(还有泛型),where就是一个用于扩展IQueryable的扩展方法。

Chapter 4: yield return关键字词组能够将本身不是可迭代集合的对象做成可迭代集合。它不能出现在catch块中,也不能出现在带有一个或多个catch子句的try块中。yield语句不能出现在匿名方法中。

Chapter 5:

1.

delegate void FunctionPointer(string str);

FunctionPointer fp = s=>Console.WriteLine(s);

fp("hello world");

2.

System.Action<string> fp = s=>Console.WriteLine(s);

fp("hello world");

Func用于在参数上执行一个操作并返回一个值;

Predicate用于定义一组条件并确定参数是否符合这些条件。

原文地址:https://www.cnblogs.com/qingxia/p/1941061.html