LINQ准备篇--学习linq的资料和笔记(一)

学习linq 的前提知识:

(1).var 隐式推断类型,

VAR 是3.5新出的一个定义变量的类型
其实也就是弱化类型的定义
VAR可代替任何类型
编译器会根据上下文来判断你到底是想用什么类型的

使用var定义变量时有以下四个特点:

1. 必须在定义时初始化。也就是必须是var s = “abcd”形式,而不能是如下形式:
var s;
s = “abcd”;

2. 一但初始化完成,就不能再给变量赋与初始化值类型不同的值了。

3. var要求是局部变量。

4. 使用var定义变量和object不同,它在效率上和使用强类型方式定义变量完全一样。

(2).扩展方法

下一个与LINQ密切相关的C# 3.0语言功能是扩展方法(Extension method)。在这之前,一旦一个类型被编译进.NET程序集后,我们便不能再修改该类型的定义了。为该类型添加、修改、删除成员的唯一办法就是修改类型的定义代码。

但有时候,当需要为类型添加新功能但并不拥有类型的已有代码时,比如,我们想要为.NET库类型List添加自定义的dump方法时,该怎么做呢,答案是扩展方法。扩展方法允许在不修改类型定义的情况下,让该类型获得功能上的扩展。

定义扩展方法

当定义一个扩展方法时,第一个限制就是必须把方法定义在静态类中,因此每一个扩展方法也必须声明为静态的。第二个限制是扩展方法要用this关键字对第一个参数进行修饰,这个参数也就是我们希望进行扩展的类型。

比如下面的扩展方法允许.NET基类库中的所有对象都拥有全新的方法DisplayDefiningAssembly()。

static class MyExtensions
        {
            // 本方法允许任何对象显示它所处的程序集
            public static void DisplayDefiningAssemlby(this object obj)
            {
                Console.WriteLine("{0} is defined in: \n\t {1}\n",
                    obj.GetType().Name,
                    System.Reflection.Assembly.GetAssembly(obj.GetType()));
            }
        }

调用扩展方法

我们有两种方式来使用扩展方法,第一种是在实例层次上调用扩展方法,第二种是静态调用扩展方法。

public void UsingExtensionMethods()
        {
            int myInt = 12345;
 
            // 1. 在实例层次上调用扩展方法
            myInt.DisplayDefiningAssemlby();
 
            // 2. 静态调用扩展方法
            MyExtensions.DisplayDefiningAssemlby(myInt);
        }

实例上,通过一个对象调用它的扩展方法只是编译器的烟幕弹效果而已,背后编译器会转换成静态方法的调用。

其他注意事项

上面说到,扩展方法本质上是可以从扩展类型的实例上调用的静态方法。所以它和普通的方法是不一样的,扩展方法不能直接访问扩展类型的成员,从另外一个角度讲,扩展方法即不是直接修改,也不是继承。

另外一个要注意的地方是:虽然表面上扩展方法是全局的,但其实他们受制于所处的命名空间,要使用在其他命名空间中定义的扩展方法时,我们首先需要导入该命名空间。


(3).对象初始化器

 
 
 
  1. User user = 
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
LINQ中文学习资料LINQ 随身参考手册,其中LINQ 随身参考手册是英文版的,但内容不错。 LINQ 随身参考手册介绍:   Ready to take advantage of LINQ with C# 3.0? This guide has the detail you need to grasp Microsoft’s new querying technology, and concise explanations to help you learn it quickly. And once you begin to apply LINQ, the book serves as an on-the-job reference when you need immediate reminders. All the examples in the LINQ Pocket Reference are preloaded into LINQPad, the highly praised utility that lets you work with LINQ interactively. Created by the authors and free to download, LINQPad will not only help you learn LINQ, it will have you thinking in LINQ. This reference explains: LINQ’s key concepts, such as deferred execution, iterator chaining, and type inference in lambda expressions The differences between local and interpreted queries C# 3.0′s query syntax in detail-including multiple generators, joining, grouping, query continuations, and more Query syntax versus lambda syntax, and mixed syntax queries Composition and projection strategies for complex queries All of LINQ’s 40-plus query operators How to write efficient LINQ to SQL queries How to build expression trees from scratch All of LINQ to XML’s types and their advanced useLINQ promises to be the locus of a thriving ecosystem for many years to come. This small book gives you a huge head start. “The authors built a tool (LINQPad) that lets you experiment with LINQ interactively in a way that the designers of LINQ themselves don’t support, and the tool has all kinds of wonderful features that LINQ, SQL and Regular Expression programmers alike will want to use regularly long after they’ve read the book.” -Chris Sells, Connected Systems Program Manager, Microsoft   About the Author   Joseph Albahari is a core C# design architect at Egton Medical Information Systems, the largest primary healthcare software supplier in the UK. He has been developing large-scale enterprise applications on .NET and other platforms for more than 15 years, working in medical, telecommunication and education industries. Joseph specializes in writing custom components and controls, and has designed application component frameworks for three companies.   Ben Albahari is currently involved in the bioinformatics business. He was a Program Manager at Microsoft for 5 years, where he worked on several projects, including the .NET Compact Framework and ADO.NET.   He was the cofounder of Genamics, a provider of tools for C# and J++ programmers, as well as software for DNA and protein sequence analysis. He is a co-author of C# Essentials, the first C# book from O’Reilly, and of previous editions of C# in a Nutshell.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值