<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>General .NET</title>
        <link>http://geekswithblogs.net/argot/category/10809.aspx</link>
        <description>General .NET</description>
        <language>en-AU</language>
        <copyright>argot</copyright>
        <managingEditor>bruceworld@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Type constraints sadness</title>
            <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>
    </channel>
</rss>
