- Lamba Expressions
Outer variables(Captured variables)
- Lamba Expression can reference local variables and paramaters of the method in which it is defined. In the following example, factor is called outer variable(captured variables).
void Foo(){
int factor = 2;
Func<int, int> multiplier = n => n * factor;
} - A captured variable is evaluated when the Lamba expression is invoked, not evaluated when it's been captured.
- A captured variable's lifetime is extended to that of delegate captrues it.
FuncFoo(){ int i = 0; return ()=>i++;}
static void Main(){
Funcfoo = Foo();
Console.WriteLine(foo());//0
Console.WriteLine(foo());//1
}
- Anonymous Methods
- Sample:
delegate int AnonymousFunc(int i);
AnonymousFunc = (int x) => {return x*x;}; - Anonymous methods captures variables like Lamba Expressions do.
- Sample:
- Extension Methods
- Extension methods extend existing types with new methods, without altering original type.
- Syntax:
public static class StringExtensions{
public static bool IsNullOrEmpty(this String s){
return (s == null s.CompareTo(String.Empty) == 0);
}...} - A compatible instance method always take precedence over an extended version.
class Test{
void Foo(object a){...} //this method always take precedence
}
static class Extensions{
void Foo(this Test t,int x){...}
}
- Anonymous Types
- A simple class created on the fly
- Syntax:
var person = new {Name="Michael",Age=32,Class="Programmer"};
int age = 32;
var person2 = new {Name="Michael",Age = age; Class="Programmer"}; - Anonymous Types are used primarily when writing LINQ
2008年3月17日
[Reading Notes]Reading Notes for [C# 3.0 Packet Reference] - 1
訂閱:
張貼留言 (Atom)
Blog Archive
-
▼
2008
(30)
-
▼
3月
(10)
- [SQL2005][SSIS]DTS_E_OLEDBERROR when executing SSI...
- [IIS] When browse IIS 6.0 web site using FQDN, a H...
- [Reading Notes]Reading Notes for [C# 3.0 Packet Re...
- [VS2008]"Requested Registry Access not allowed" Er...
- [IIS][.Net]Execute .Net assembly in a shared locat...
- [Reading Notes]Reading notes for [Programming C# 3...
- [BizTalk][RosettaNet]A4RN configuration on cluster...
- [IIS]IIS Website with a virtual directory from sha...
- [SQL 2005]Xml data type in SQL 2005
- [Cluster]How to make your window service a cluster...
-
▼
3月
(10)
沒有留言:
張貼留言