<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Bruce Ge</title>
        <link>http://geekswithblogs.net/argot/Default.aspx</link>
        <description> </description>
        <language>en-AU</language>
        <copyright>argot</copyright>
        <managingEditor>bruceworld@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Bruce Ge</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/argot/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Type constraints sadness</title>
            <category>General .NET</category>
            <category>C#</category>
            <link>http://geekswithblogs.net/argot/archive/2009/11/21/type-constraints-sadness.aspx</link>
            <description>&lt;div style="margin: 0cm 0cm 10pt"&gt;Although delegate type and Enum type are class types, we can not inherit from them as they are sealed classes after compile,&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;delegate &lt;span style="color: blue"&gt;void&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DelegateTestType&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;//Compile time error&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;myTest : DelegateTestType&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;{&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span&gt;     ...&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;}&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;we can not make it as a type constraint either on class or method:&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;//Compile time error&lt;/div&gt;
&lt;div style="text-justify: inter-ideograph; text-align: justify; line-height: normal"&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;myTest&amp;lt;T&amp;gt; &lt;span style="color: blue"&gt;where&lt;/span&gt; T : DelegateTestType&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-justify: inter-ideograph; text-align: justify; line-height: normal"&gt;{&lt;/div&gt;
&lt;div style="text-justify: inter-ideograph; text-align: justify; line-height: normal"&gt;&lt;span&gt;       &lt;/span&gt;&lt;/div&gt;
&lt;div style="text-justify: inter-ideograph; text-align: justify; line-height: normal"&gt;}&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: 130%"&gt;&lt;span style="line-height: 130%; color: black; font-size: 11pt"&gt;From the &lt;a href="http://download.microsoft.com/download/9/8/f/98fdf0c7-2bbd-40d3-9fd1-5a4159fa8044/CSharp%202.0%20Specification_Sept_2005.doc"&gt;&lt;span style="color: #0077cc; text-decoration: none; text-underline: none"&gt;C# 2.0 specification&lt;/span&gt;&lt;/a&gt; we can read (20.7, Constraints):&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%"&gt; &lt;/div&gt;
&lt;div style="line-height: 130%; margin: 0cm 0cm 12pt"&gt;&lt;span style="color: black"&gt;A class-type constraint must satisfy the following rules:&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%; text-indent: -18pt; margin: 0cm 0cm 10pt 22.5pt"&gt;&lt;span style="line-height: 130%; color: black; font-size: 10pt"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black"&gt;The type must be a class type.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%; text-indent: -18pt; margin: 0cm 0cm 10pt 22.5pt"&gt;&lt;span style="line-height: 130%; color: black; font-size: 10pt"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black"&gt;The type must not be sealed.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%; text-indent: -18pt; margin: 0cm 0cm 10pt 22.5pt"&gt;&lt;span style="line-height: 130%; color: black; font-size: 10pt"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black"&gt;The type must not be one of the following types: System.Array, System.Delegate, System.Enum, or System.ValueType&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%; text-indent: -18pt; margin: 0cm 0cm 10pt 22.5pt"&gt;&lt;span style="line-height: 130%; color: black; font-size: 10pt"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black"&gt;The type must not be object. Because all types derive from object, such a constraint would have no effect if it were permitted.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 130%; text-indent: -18pt; margin: 0cm 0cm 10pt 22.5pt"&gt;&lt;span style="line-height: 130%; color: black; font-size: 10pt"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black"&gt;At most one constraint for a given type parameter can be a class type.&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;In c# world we have to use some indirect ways, like type casting, wrapper class or examine the type is Delegate type, it is still a problem in C# 4.0, this is a bit sad as C++ can do it more than happily!&lt;/div&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/136451.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/11/21/type-constraints-sadness.aspx</guid>
            <pubDate>Sat, 21 Nov 2009 11:17:58 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/136451.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/11/21/type-constraints-sadness.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/136451.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Delegate in anonymous type</title>
            <category>C#</category>
            <link>http://geekswithblogs.net/argot/archive/2009/11/10/delegate-in-anonymous-type.aspx</link>
            <description>&lt;p&gt;&lt;font size="3"&gt;We all know the usage of the anonymous type:&lt;/font&gt;&lt;/p&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="LINE-HEIGHT: 115%; COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24 };&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;font size="3"&gt;What if we want to add a anonymous delegate or Lamba to it?&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;Like:&lt;/font&gt;&lt;/p&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24, behaviour=&lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age; } };&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;font size="3"&gt;or&lt;/font&gt; &lt;/p&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24, behaviour = (name, age) =&amp;gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Format( &lt;span style="COLOR: #a31515"&gt;"Hello, {0}! You are {1} years old."&lt;/span&gt;, name, age ) };&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;font size="3" face=""&gt;The above two will cause compile time error, because anonymous type need anonymous type property, not the anonymous method or lambda expression.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;There is some way to do it:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;1. define a Func:&lt;/font&gt;&lt;font face=""&gt;&lt;span style="COLOR: #2b91af; FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: #2b91af; FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: #2b91af; FONT-SIZE: 10pt"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/font&gt; &lt;/p&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Func&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt; behav =&lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;               &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            };&lt;/span&gt;&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;then we can do it:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; {Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24, behaviour = behav };&lt;/span&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;2. declare a delegate:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;delegate&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;behav&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age);&lt;/span&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;then we can do:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24, behaviour = (&lt;span style="COLOR: #2b91af"&gt;behav&lt;/span&gt;)&lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age; } };&lt;/span&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3" face=""&gt;or&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;var&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; obj = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { Name = &lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, Age = 24, behaviour = (&lt;span style="COLOR: #2b91af"&gt;behav&lt;/span&gt;)((name, age) =&amp;gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="COLOR: #a31515"&gt;"Hello, {0}! You are {1} years old."&lt;/span&gt;, name, age)) };&lt;/span&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;This is the demonstration only, if we want to use &lt;font face=""&gt;IEnumerable&amp;lt;AnonymousType&amp;gt;, as anonymous type can only be cast as object, there is article &lt;a href="http://mikehadlow.blogspot.com/2008/04/extension-properties-for-easy.html"&gt;here&lt;/a&gt; to describe how to use it. Here is another solution:&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;font size="3"&gt;&lt;font face=""&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;class&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;&amp;lt;TName, TAge, TBehaviour&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; Collection { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Foo()&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        Collection = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; AddValue(TName name, TAge age, TBehaviour func)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;strong&gt;&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; cust = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { CustomerName = name, Customerage = age, Behaviour = func };&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        Collection.Add(cust);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;static&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; T Cast&amp;lt;T&amp;gt;(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj, T type)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (T)obj;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;static&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] args)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt; behav =&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; _foo = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, 24, behav);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"Tom"&lt;/span&gt;, 26, &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age; });&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"Tim"&lt;/span&gt;, 30, (name, age) =&amp;gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="COLOR: #a31515"&gt;"Hello, {0}! You are {1} years old."&lt;/span&gt;, name, age));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; typeinfo = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { CustomerName = &lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;, Customerage = 0, Behaviour = behav };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; customer &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; _foo.Collection)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; typed = Cast(customer, typeinfo);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(typed.Behaviour(typed.CustomerName, typed.Customerage));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;}
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;&lt;font face=""&gt;Otherwise we can do it without anonymous type as below:&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; &lt;/font&gt; &lt;/p&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 10pt"&gt;        public&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;lt;TCustomerName, TCustomerAge, TFunction&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; TCustomerName CustomerName { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; TCustomerAge Customerage { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; TFunction Behaviour { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;&amp;lt;TName, TAge, TBehaviour&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;lt;TName, TAge, TBehaviour&amp;gt;&amp;gt; Collection { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Foo()&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                Collection = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;lt;TName, TAge, TBehaviour&amp;gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; AddValue(TName name, TAge age, TBehaviour func)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                &lt;strong&gt;&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; cust = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;lt;TName, TAge, TBehaviour&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;strong&gt;                    {CustomerName = name, Customerage = age, Behaviour = func};&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                Collection.Add(cust);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] args)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt; behav =&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; _foo = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, 24, behav);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"Tom"&lt;/span&gt;, 26, &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; age) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age; });&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"Tim"&lt;/span&gt;, 30, (name, age) =&amp;gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="COLOR: #a31515"&gt;"Hello, {0}! You are {1} years old."&lt;/span&gt;, name, age));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; customer &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; _foo.Collection)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(customer.Behaviour(customer.CustomerName, customer.Customerage));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;/span&gt;&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In .Net 4.0, you even can do it more flexible by using dynamic type:&lt;/p&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 9.5pt"&gt;class&lt;/span&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; Collection { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Foo()&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        Collection = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; AddValue(&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; age, &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;&amp;gt; func)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; cust = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { CustomerName = name, Customerage = age, Behaviour = func };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        Collection.Add(cust);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 9.5pt"&gt;static&lt;/span&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt; T Cast&amp;lt;T&amp;gt;(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj, T type)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (T)obj;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="COLOR: blue; FONT-SIZE: 9.5pt"&gt;static&lt;/span&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] args)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;&amp;gt; behav =&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; age)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; _foo = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Foo&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    _foo.AddValue(&lt;span style="COLOR: #a31515"&gt;"John"&lt;/span&gt;, &lt;span style="COLOR: brown"&gt;24&lt;/span&gt;, behav);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    _foo.AddValue(&lt;strong&gt;&lt;u&gt;&lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;.Now&lt;/u&gt;&lt;/strong&gt;, &lt;span style="COLOR: brown"&gt;26&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; name, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; age) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"Customer Name: "&lt;/span&gt; + name + &lt;span style="COLOR: #a31515"&gt;", Age: "&lt;/span&gt; + age; });&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    _foo.AddValue(&lt;strong&gt;&lt;u&gt;&lt;span style="COLOR: #2b91af"&gt;Int32&lt;/span&gt;.MaxValue&lt;/u&gt;&lt;/strong&gt;, &lt;span style="COLOR: brown"&gt;30&lt;/span&gt;, (name, age) =&amp;gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="COLOR: #a31515"&gt;"Hello, {0}! You are {1} years old."&lt;/span&gt;, name, age));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; typeinfo = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; { CustomerName = (&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;)&lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;, Customerage = (&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;)&lt;span style="COLOR: brown"&gt;0&lt;/span&gt;, Behaviour = behav };&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; customer &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; _foo.Collection)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; typed = Cast(customer, typeinfo);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;        &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(typed.Behaviour(typed.CustomerName, typed.Customerage));&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;    &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.Read();&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt"&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;code&gt;&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;code&gt;&lt;/code&gt;&lt;/span&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/136169.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/11/10/delegate-in-anonymous-type.aspx</guid>
            <pubDate>Tue, 10 Nov 2009 02:42:38 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/136169.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/11/10/delegate-in-anonymous-type.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/136169.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Sync Framework - Inefficient ApplyChanges</title>
            <category>Sync Framework</category>
            <link>http://geekswithblogs.net/argot/archive/2009/10/27/sync-framework---inefficient-applychanges.aspx</link>
            <description>&lt;p&gt;for the ado database sync, the method &lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;public virtual SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet,SyncSession syncSession)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;on server side is not efficient, as it receive the changed data from client side, but it again sent it back to client within the SyncContext. &lt;/p&gt;
&lt;p&gt;In the returned SyncContext object, I found DataSet and &lt;font face=""&gt;GroupProgress.Changes is almost the same as input dataset. by verifying the code inside sync framework, I found:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;
&lt;table style="MARGIN-BOTTOM: 0px" cellspacing="0" cellpadding="0" width="100%"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td style="PADDING-BOTTOM: 4px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; PADDING-TOP: 4px" valign="top" colspan="2"&gt;
            &lt;pre&gt;&lt;font color="#1000a0"&gt;internal&lt;/font&gt; &lt;a title="Microsoft.Synchronization.Data.SyncGroupMetadata" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncGroupMetadata"&gt;SyncGroupMetadata&lt;/a&gt; &lt;strong&gt;&lt;a class="bold" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/ResetProivderState(Microsoft.Synchronization.Data.SyncGroupMetadata,System.Data.DataSet):Microsoft.Synchronization.Data.SyncGroupMetadata"&gt;ResetProivderState&lt;/a&gt;&lt;/strong&gt;(&lt;a title="Microsoft.Synchronization.Data.SyncGroupMetadata" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncGroupMetadata"&gt;SyncGroupMetadata&lt;/a&gt; groupMetadata, &lt;a title="System.Data.DataSet" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://System.Data:2.0.0.0:b77a5c561934e089/System.Data.DataSet"&gt;DataSet&lt;/a&gt; dataSet)
{
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="object Microsoft.Synchronization.Data.Server.DbServerSyncProvider._rawOldAnchor;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_rawOldAnchor:Object"&gt;_rawOldAnchor&lt;/a&gt; = &lt;font color="#800000"&gt;null&lt;/font&gt;;
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="object Microsoft.Synchronization.Data.Server.DbServerSyncProvider._rawNewAnchor;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_rawNewAnchor:Object"&gt;_rawNewAnchor&lt;/a&gt; = &lt;font color="#800000"&gt;null&lt;/font&gt;;
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="object Microsoft.Synchronization.Data.Server.DbServerSyncProvider._rawMaxAnchor;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_rawMaxAnchor:Object"&gt;_rawMaxAnchor&lt;/a&gt; = &lt;font color="#800000"&gt;null&lt;/font&gt;;
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="SyncContext Microsoft.Synchronization.Data.Server.DbServerSyncProvider._syncContext;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_syncContext:Microsoft.Synchronization.Data.SyncContext"&gt;_syncContext&lt;/a&gt; = &lt;font color="#1000a0"&gt;new&lt;/font&gt; &lt;a title="Microsoft.Synchronization.Data.SyncContext.SyncContext();" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncContext/.ctor()"&gt;SyncContext&lt;/a&gt;();
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="SyncContext Microsoft.Synchronization.Data.Server.DbServerSyncProvider._syncContext;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_syncContext:Microsoft.Synchronization.Data.SyncContext"&gt;_syncContext&lt;/a&gt;.&lt;a title="DataSet Microsoft.Synchronization.Data.SyncContext.DataSet { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncContext/property:DataSet:System.Data.DataSet"&gt;DataSet&lt;/a&gt; = &lt;a title="DataSet dataSet; // Parameter"&gt;dataSet&lt;/a&gt;;
    &lt;a title="Microsoft.Synchronization.Data.SyncGroupMetadata" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncGroupMetadata"&gt;SyncGroupMetadata&lt;/a&gt; &lt;strong&gt;metadata&lt;/strong&gt; = &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="SyncGroupMetadata Microsoft.Synchronization.Data.Server.DbServerSyncProvider.GenerateOrderedGroupMetadata(SyncGroupMetadata groupMetadata);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/GenerateOrderedGroupMetadata(Microsoft.Synchronization.Data.SyncGroupMetadata):Microsoft.Synchronization.Data.SyncGroupMetadata"&gt;GenerateOrderedGroupMetadata&lt;/a&gt;(&lt;a title="SyncGroupMetadata groupMetadata; // Parameter"&gt;groupMetadata&lt;/a&gt;);
    &lt;font color="#1000a0"&gt;this&lt;/font&gt;.&lt;a title="SyncContext Microsoft.Synchronization.Data.Server.DbServerSyncProvider._syncContext;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data.Server:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.Server.DbServerSyncProvider/_syncContext:Microsoft.Synchronization.Data.SyncContext"&gt;_syncContext&lt;/a&gt;.&lt;a title="SyncGroupProgress Microsoft.Synchronization.Data.SyncContext.GroupProgress { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncContext/property:GroupProgress:Microsoft.Synchronization.Data.SyncGroupProgress"&gt;GroupProgress&lt;/a&gt; = &lt;font color="#1000a0"&gt;new&lt;/font&gt; &lt;a title="Microsoft.Synchronization.Data.SyncGroupProgress.SyncGroupProgress(SyncGroupMetadata, DataSet);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Microsoft.Synchronization.Data:3.0.0.0:89845dcd8080cc91/Microsoft.Synchronization.Data.SyncGroupProgress/.ctor(Microsoft.Synchronization.Data.SyncGroupMetadata,System.Data.DataSet)"&gt;SyncGroupProgress&lt;/a&gt;(&lt;a title="SyncGroupMetadata metadata // Local Variable"&gt;metadata&lt;/a&gt;, &lt;a title="DataSet dataSet; // Parameter"&gt;dataSet&lt;/a&gt;);
    &lt;font color="#1000a0"&gt;return&lt;/font&gt; &lt;a title="SyncGroupMetadata metadata // Local Variable"&gt;metadata&lt;/a&gt;;
}
&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;so I use the code below:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;SyncContext context = _serverSyncProvider.ApplyChanges(groupMetadata, dataSet, syncSession);&lt;br /&gt;
context.DataSet = null;&lt;br /&gt;
&lt;/font&gt;&lt;font face=""&gt;context.GroupProgress.Changes.Clear();&lt;br /&gt;
&lt;/font&gt;&lt;font face=""&gt;return context; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;to make the size smaller.&lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135732.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/27/sync-framework---inefficient-applychanges.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 05:54:08 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135732.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/27/sync-framework---inefficient-applychanges.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135732.aspx</wfw:commentRss>
        </item>
        <item>
            <title>WCF Message Compression - Gzip, 7zip</title>
            <category>WCF</category>
            <link>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx</link>
            <description>&lt;p&gt;I was using &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/cc138373.aspx"&gt;Gzip Encoder&lt;/a&gt; to compress wcf message, it surprised me that sometimes the compression message size is even bigger than the original size, so I looked the code, I found within &lt;font face=""&gt;GZipMessageEncoderFactory.cs, the method "&lt;font face=""&gt;CompressBuffer" in the &lt;font face=""&gt;GZipMessageEncoderFactory class is not quite right. it was like this originally:&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
   ....&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
                                                       &lt;strong&gt;bufferedBytes.Length - messageOffset&lt;/strong&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;if we use the method above, the count of the new ArraySegment&amp;lt;T&amp;gt; will be the bufferedBytes, as &lt;font face=""&gt;BufferManager using &lt;font face=""&gt;TakeBuffer method to grab a buffer which is the nearest bigger 2&lt;sup&gt;N&lt;/sup&gt; byte, e.g. if input param is 522 bytes for &lt;font face=""&gt;TakeBuffer method, it will grab a buffer which has 1024 bytes. And the constructor of ArraySegment will take the third parameter(bold above) to reflects the actual size of the ArraySegment, if size of the &lt;font face=""&gt;bufferedBytes(first params) is less than third parameter, it will fill byte0 to the rest. that is why sometimes the size is even bigger than the original size. &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;it should be like this:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
   ....&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
                       &lt;strong&gt;compressedBytes.Length&lt;/strong&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Also I found the Gzip compression doesn't have high compression ratio, I decided to use 7zip and gzip combination then, I found a good article about 7zip: &lt;a href="http://www.eggheadcafe.com/tutorials/aspnet/064b41e4-60bc-4d35-9136-368603bcc27a/7zip-lzma-inmemory-com.aspx"&gt;7Zip (LZMA) In-Memory Compression with C#&lt;/a&gt;, after adding it to my project, I can use it straight forward:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
 using (var memoryStream = new MemoryStream())&lt;br /&gt;
 {&lt;br /&gt;
  var zipperStream = new GZipStream(memoryStream, CompressionMode.Compress, true);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  using (zipperStream)&lt;br /&gt;
   zipperStream.Write(buffer.Array,&lt;br /&gt;
      buffer.Offset, buffer.Count);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  byte[] compressedBytes1 = memoryStream.ToArray();&lt;br /&gt;
  byte[] compressedBytes = SevenZip.Compression.LZMA.SevenZipHelper.Compress(compressedBytes1);&lt;br /&gt;
  byte[] bufferedBytes = bufferManager.TakeBuffer(&lt;br /&gt;
     compressedBytes.Length + messageOffset);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset,&lt;br /&gt;
     compressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  bufferManager.ReturnBuffer(buffer.Array);&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
     compressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
 }&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;//Helper method to decompress an array of bytes&lt;br /&gt;
private static ArraySegment&amp;lt;byte&amp;gt; DecompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager)&lt;br /&gt;
{&lt;br /&gt;
 var memoryStream1 = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count - buffer.Offset);&lt;br /&gt;
 var memoryStream=new MemoryStream(SevenZip.Compression.LZMA.SevenZipHelper.Decompress(memoryStream1.ToArray()));&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; var decompressedStream = new MemoryStream();&lt;br /&gt;
 int totalRead = 0;&lt;br /&gt;
 int blockSize = 1024;&lt;br /&gt;
 byte[] tempBuffer = bufferManager.TakeBuffer(blockSize);&lt;br /&gt;
 using (var gzStream = new GZipStream(memoryStream, CompressionMode.Decompress))&lt;br /&gt;
 {&lt;br /&gt;
  while (true)&lt;br /&gt;
  {&lt;br /&gt;
   int bytesRead = gzStream.Read(tempBuffer, 0, blockSize);&lt;br /&gt;
   if (bytesRead == 0)&lt;br /&gt;
    break;&lt;br /&gt;
   decompressedStream.Write(tempBuffer, 0, bytesRead);&lt;br /&gt;
   totalRead += bytesRead;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 bufferManager.ReturnBuffer(tempBuffer);&lt;br /&gt;
   &lt;br /&gt;
 byte[] decompressedBytes = decompressedStream.ToArray();&lt;br /&gt;
 byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset);&lt;br /&gt;
 Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset);&lt;br /&gt;
 Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);&lt;br /&gt;
 bufferManager.ReturnBuffer(buffer.Array);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135731.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 05:20:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135731.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135731.aspx</wfw:commentRss>
        </item>
        <item>
            <title>WCF ReliableSession Retry Interval</title>
            <category>WCF</category>
            <link>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx</link>
            <description>&lt;p&gt;Been using &lt;span class="identifier"&gt;ReliableSessionBindingElement within &lt;/span&gt;WCF, I found we can not set the retry interval, as we only can set MaxRetryCount. WCF uses an internal algorithm to determine when to retransmit, based on a computed average round-trip time. The initial retry time is computed based on the measured roundtrip time of establishing the session. The retransmission algorithm doubles the delay with every attempt, for example, if we set the MaxRetryCount to 8(default), if WCF determines the initial retry time to be 2 seconds, then 8th retry will set the retry ackonwledgement time to be 2^8 which is 512 seconds. We can not change the acknowledgement interval manually. so if you see from &lt;font face=""&gt;Fiddler, WCF &lt;font face=""&gt;Visualizer&lt;/font&gt; or Trace Viewer, it is not surprise to see some messages been sent several times if you use &lt;span class="identifier"&gt;ReliableSession. There is no other way to avoid this except making server stateless (disable ReliableSession)&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span class="identifier"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135730.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 04:45:50 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135730.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135730.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Sync Framework Common Practise of ADO.NET</title>
            <category>Sync Framework</category>
            <link>http://geekswithblogs.net/argot/archive/2009/10/18/sync-framework-common-practise-of-ado.net.aspx</link>
            <description>&lt;p&gt;&lt;font face=""&gt;I have been working on database synchronization for a couple of weeks, and seems it at the final testing stage, I am using &lt;a href="http://msdn.microsoft.com/en-us/sync/default.aspx"&gt;Microsoft Sync Framework 2.0&lt;/a&gt;, the DB server is SQL server 2008 Standard, and clients are SQL Server 2008 Express as I use Change Tracking to track DB changes instead of TombStone Tables and Guid tracking columns, I use WCF+IIS+SSL to host server side service, the binding uses Gzip binary encoding.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;The reason we are not using SQL Server Replication is because our tables need dynamic filters and some of them logic is quite complicated (we need to download the certain data to client according to the client name and setting), so when Replication try to generate the snapshot, it takes quite long time and CPU usage is quite high.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;Coding didn't take me so long to finish (main part is write customized filter queries) but I realize some of the sync tables using bigint as primary key instead of Guid, so I have to assign a new seed to each client to avoid confliction. This new seed number will download and apply when clients reinitialize after schema download and applied.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;Also, I found after "Applychanges" has been called, some tables identities will be changed, because in the "InsertCommand" of the Adaptor, using "SET IDENTITY_INSERT [tableName] ON", this will cause "If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.", so we use a new table to hold the current identity for tables before applychanges and set it back after applychanges.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;I have found when uploading changes to server side, the dataset contains all changed data, and after &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.server.dbserversyncprovider.applychanges.aspx"&gt;ApplyChanges&lt;/a&gt; called, the return object &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.synccontext(SQL.105).aspx"&gt;SyncContext&lt;/a&gt; still contains the dataset which sent to server, this is very inefficient, also when some changes apply failed, the return size grows very big because the &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.synccontext.groupprogress(SQL.105).aspx"&gt;GroupProgress&lt;/a&gt; inside &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.synccontext(SQL.105).aspx"&gt;SyncContext&lt;/a&gt; contains all dataset info plus failed table and row info, this makes memory growing very fast on server side as well as transferring.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;Server side need to maintain a session as the the clients call is each client based, so the WCF interface is like this:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;[ServiceContract(SessionMode = SessionMode.Required)]&lt;br /&gt;
    public interface IServiceForSync&lt;br /&gt;
    {&lt;br /&gt;
        [OperationContract(IsInitiating = true)]&lt;br /&gt;
        bool InitialiseSetting();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        SyncSchema GetSchema(Collection&amp;lt;string&amp;gt; tableNames, SyncSession syncSession);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        SyncServerInfo GetServerInfo(SyncSession syncSession);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        SyncInfo GetSyncInfo(string SyncHostName, long rpuid);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        List&amp;lt;String&amp;gt; GetInitSchema();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        List&amp;lt;String&amp;gt; GetUpdateSchema(string SyncHostName);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsInitiating = false)]&lt;br /&gt;
        bool ResetStatus(string SyncHostName);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;        [OperationContract(IsTerminating = true)]&lt;br /&gt;
        void EndSession(); &lt;br /&gt;
    }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The Service side binding in the web.config is like this:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;   &amp;lt;customBinding&amp;gt;&lt;br /&gt;
    &amp;lt;binding name="ISyncServer" receiveTimeout="00:30:00" sendTimeout="00:30:00"&amp;gt;&lt;br /&gt;
     &amp;lt;gzipMessageEncoding innerMessageEncoding="binaryMessageEncoding"/&amp;gt;&lt;br /&gt;
     &amp;lt;reliableSession ordered="true" inactivityTimeout="00:10:00" maxPendingChannels="128"/&amp;gt;&lt;br /&gt;
     &amp;lt;httpsTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" maxReceivedMessageSize="2000000000" authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True"/&amp;gt;&lt;br /&gt;
    &amp;lt;/binding&amp;gt;&lt;br /&gt;
   &amp;lt;/customBinding&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;On client side the remoteServer of the syncAgent will be the proxy, and I use one SyncGroup as any exception happens, it will reverse back to original.&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;public class SqlExpressClientSyncProvider : ClientSyncProvider&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;and in the ApplyChanges of the ClientSyncProvider, we need to &lt;font face=""&gt;Map SyncDirection from client point of view to our internal server point of view, so part of code is like:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; foreach (var tableMetadata in groupMetadata.TablesMetadata)&lt;br /&gt;
            {&lt;br /&gt;
                switch (tableMetadata.SyncDirection)&lt;br /&gt;
                {&lt;br /&gt;
                    case SyncDirection.Snapshot:&lt;br /&gt;
                    case SyncDirection.DownloadOnly:&lt;br /&gt;
                        tableMetadata.SyncDirection = SyncDirection.UploadOnly;&lt;br /&gt;
                        break;&lt;br /&gt;
                    case SyncDirection.UploadOnly:&lt;br /&gt;
                        tableMetadata.SyncDirection = SyncDirection.DownloadOnly;&lt;br /&gt;
                        break;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;in GetChanges, we need to swap anchors:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;public override SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession)&lt;br /&gt;
        {&lt;br /&gt;
            // neet to set the LastReceivedAnchor as the LastSentAnchor since &lt;br /&gt;
            // DbServerSyncProvider operates from the server's perspective, so&lt;br /&gt;
            // we swap the two fields temporarily. &lt;br /&gt;
            foreach (var metaTable in groupMetadata.TablesMetadata)&lt;br /&gt;
            {&lt;br /&gt;
                var temp = metaTable.LastReceivedAnchor;&lt;br /&gt;
                metaTable.LastReceivedAnchor = metaTable.LastSentAnchor;&lt;br /&gt;
                metaTable.LastSentAnchor = temp;&lt;br /&gt;
            }&lt;br /&gt;
           &lt;br /&gt;
            var context = _dbSyncProvider.GetChanges(groupMetadata, syncSession);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;br /&gt;
            if (_bEnableTimeAdjusting.HasValue &amp;amp;&amp;amp; _bEnableTimeAdjusting.Value)&lt;br /&gt;
                AdjustDSTimeZone(context.DataSet, "CLIENT"); //-TN - adjust all datetime columns before sync.&lt;br /&gt;
            //swap them back for consistency&lt;br /&gt;
            foreach (var metaTable in groupMetadata.TablesMetadata)&lt;br /&gt;
            {&lt;br /&gt;
                var temp = metaTable.LastReceivedAnchor;&lt;br /&gt;
                metaTable.LastReceivedAnchor = metaTable.LastSentAnchor;&lt;br /&gt;
                metaTable.LastSentAnchor = temp;&lt;br /&gt;
            }&lt;br /&gt;
            return context;&lt;br /&gt;
        }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135541.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/18/sync-framework-common-practise-of-ado.net.aspx</guid>
            <pubDate>Sun, 18 Oct 2009 13:30:37 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135541.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/18/sync-framework-common-practise-of-ado.net.aspx#feedback</comments>
            <slash:comments>9</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135541.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
