<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jon Skeet: Coding Blog : CSharpDevCenter, Google</title><link>http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/Google/default.aspx</link><description>Tags: CSharpDevCenter, Google</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Lessons learned from Protocol Buffers, part 4: static interfaces</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx</link><pubDate>Fri, 29 Aug 2008 18:50:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646206</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646206</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646206</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; During this entire post, I will use the word &lt;i&gt;static&lt;/i&gt; to mean &amp;quot;relating to a type instead of an instance&amp;quot;. This isn&amp;#39;t a &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Static+Methods/default.aspx"&gt;strictly accurate use&lt;/a&gt; but I believe it&amp;#39;s what most developers actually think of when they hear the word.&lt;/p&gt; &lt;p&gt;A few members of the interfaces in Protocol Buffers have no logical reason to act on instances of their types. The message interface has members to return the message&amp;#39;s type descriptor (the PB equivalent of &lt;code&gt;System.Type&lt;/code&gt;), the default instance for the message type, and a builder for the message type. The builder interface copies the first two of these, and also has a method to create a builder for a particular field. None of these touch any instance data.&lt;/p&gt; &lt;p&gt;In most cases this doesn&amp;#39;t actually cause any difficulties - we usually have an instance available when we&amp;#39;re in the PB library code, and the generated types have static properties for the default instance and the type descriptor anyway. Even so, it feels messy to have interface members which rely only on the &lt;i&gt;type&lt;/i&gt; of the implementation and not on any of the actual &lt;i&gt;data&lt;/i&gt; of the instance.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve wondered before now about the possibility of having static members in interfaces - usually when thinking about plug-in architectures - but there&amp;#39;s always been the problem of working out how to specify the type on which to call the members. Variables and other expressions usually refer to values rather than types, and &lt;code&gt;System.Type&lt;/code&gt; doesn&amp;#39;t help as it provides no compile-time knowledge of the type being referred to.&lt;/p&gt; &lt;p&gt;There&amp;#39;s one big exception to this, however: generic type parameters. I don&amp;#39;t know why it had never occurred to me before, but this is a great fit for the ability to safely call static methods on types which are unknown at compile-time. Furthermore, it could provide a great way of enforcing the presence of constructors with appropriate signatures, and even operators. Before I get too far ahead of myself, let&amp;#39;s tie the simple case to a concrete example.&lt;/p&gt; &lt;h3&gt;Creating builders from nothing&lt;/h3&gt; &lt;p&gt;In my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx"&gt;previous post&lt;/a&gt; I gave an example of a method which ideally wanted to return a new message given a &lt;code&gt;CodedInputStream&lt;/code&gt; and an &lt;code&gt;ExtensionRegistry&lt;/code&gt; (both types within Protocol Buffers, the details of which are unimportant to this example). The current code looks like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;For the purposes of this discussion I&amp;#39;ll simplify it a little, making it a generic method in a non-generic type.&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;The first parameter is a function which will return us a builder. We can&amp;#39;t simply add a &lt;code&gt;new()&lt;/code&gt; constraint to &lt;code&gt;TBuilder&lt;/code&gt; as not all geenrated builders will have a public constructor. However, we &lt;i&gt;do&lt;/i&gt; know that the &lt;code&gt;TMessage&lt;/code&gt; type has a &lt;code&gt;CreateBuilder()&lt;/code&gt; method because it implements &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt;. Unfortunately we don&amp;#39;t have an instance of &lt;code&gt;TMessage&lt;/code&gt; to call &lt;code&gt;CreateBuilder()&lt;/code&gt; on! Really, we&amp;#39;d like to be able to change the code to this: &lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (CodedInputStream input, ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = &lt;b&gt;TMessage.CreateBuilder();&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;That&amp;#39;s currently impossible, but only because we can&amp;#39;t specify static methods in interfaces. Suppose we could write:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; TBuilder CreateBuilder();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Other methods as before&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Wouldn&amp;#39;t that be useful? The value would almost entirely be for generic types or methods where the type parameter is constrained to specify the relevant interface, but that could arguably still be very handy.  &lt;h3&gt;Operators and constructors&lt;/h3&gt; &lt;p&gt;At this point hopefully the idea I mentioned earlier of being able to specify operators and constructors is quite obvious. For instance, we could make all the existing numeric types implement &lt;code&gt;IArithmetic&amp;lt;T&amp;gt;&lt;/code&gt; (where &lt;code&gt;T&lt;/code&gt; was the same type, e.g. &lt;code&gt;int : IArithmetic&amp;lt;int&amp;gt;&lt;/code&gt;):&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; +(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; -(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; /(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; *(T left, T right);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used in LINQ to Objects, for example:&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; T Sum&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source) &lt;span class="Linq"&gt;where&lt;/span&gt; T : IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T total = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T element &lt;span class="Statement"&gt;in&lt;/span&gt; source)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += element; &lt;span class="InlineComment"&gt;// Interface says we can do total + element&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; total;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;Plug-ins for a particular program could implement &lt;code&gt;IPlugin&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; (PluginHost host);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal plug-in members here&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; Title { get; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used within a PluginHost like this...&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; T CreatePlugin&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T plugin = &lt;span class="Keyword"&gt;new&lt;/span&gt; T(&lt;span class="Keyword"&gt;this&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; log.Info(&lt;span class="String"&gt;&amp;quot;Loaded plugin {0}&amp;quot;&lt;/span&gt;, plugin.Title);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; plugin;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;In fact, I&amp;#39;d imagine it would make sense to define a whole family of &lt;code&gt;IConstructable&lt;/code&gt; interfaces, along the same lines as the &lt;code&gt;Func&lt;/code&gt; and &lt;code&gt;Action&lt;/code&gt; delegate families:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T arg)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T1, T2&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T1 arg1, T2 arg2);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// etc&lt;/span&gt; &lt;/div&gt; &lt;h3&gt;Inheritance raises its ugly head&lt;/h3&gt; &lt;p&gt;There&amp;#39;s a fly in the ointment here. Normally, if a base type implements an interface, that means a type derived from it will effectively implement the interface too. That ceases to hold in all cases. You can get away with it for straightforward static methods/properties, in the same way that people often write code such as &lt;code&gt;UTF8Encoding.UTF8&lt;/code&gt; when they really just mean &lt;code&gt;Encoding.UTF8&lt;/code&gt;. However, it doesn&amp;#39;t work for constructors - they aren&amp;#39;t inherited, so you can&amp;#39;t guarantee that &lt;code&gt;Banana&lt;/code&gt; has a parameterless constructor just because &lt;code&gt;Fruit&lt;/code&gt; does. &lt;/p&gt; &lt;p&gt;This is not only a problem for one concrete class deriving from another, but also for abstract implementations of interfaces. This happens a lot in Protocol Buffers; often the interface is partially implemented by the abstract class, with the final &amp;quot;leaf&amp;quot; classes in the inheritance tree implementing outstanding members and occasionally overriding earlier implementations for the sake of efficiency. Should we be able to specify an abstract static method in the abstract class, making sure that there&amp;#39;s an appropriate implementation by the time we hit a concrete class? As it happens that would be useful elsewhere in the Protocol Buffer library, but I&amp;#39;ll admit it&amp;#39;s slightly messy. I suspect there are ways round all of these issues, even if they might sometimes involve restricting the feature to particular, common use cases. However, every niggle would add its own piece of complexity. &lt;/p&gt; &lt;p&gt;There may well be other issues which would prove challenging - and other interesting aspects such as what explicit interface implementation would mean, if anything, in the context of static members. Language experts may well be able to reel off great lists of problems - I&amp;#39;d be very interested to here the ensuing discussions. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I believe that static interface members could prove very useful in generic algorithms, particularly if operators and constructors were allowed as well as the existing member types available in interfaces. There are significant sticking points to be carefully considered, and I wouldn&amp;#39;t like to prejudge the outcome of such deliberations in terms of whether the feature would be useful enough to merit the additional language complexity involved. It feels odd to implement an interface member which is effectively only of use when the implementing type is being used as the type argument for a generic type or method, but as developers learn to think more generically that may be less of a restriction than it currently seems. &lt;/p&gt; &lt;p&gt;This post is the last in my current batch talking about Protocol Buffers. There may well be more, but it&amp;#39;s unlikely now that the Protocol Buffer port is almost entirely complete. Most of these posts have involved generics, and the current limitations of what C# allows us to express in them. I do not intend to give the impression that I&amp;#39;m dissatisfied with C# - I&amp;#39;ve just found it interesting to take a look at what lies beyond the current boundaries of the language. The one aspect of these posts which I &lt;i&gt;would&lt;/i&gt; definitely like to see addresses is that of covariant return types - the Java implementation of Protocol Buffers is significantly simpler in many ways &lt;i&gt;purely&lt;/i&gt; due to this one point.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646206" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 3: generic type relationships</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx</link><pubDate>Fri, 29 Aug 2008 18:48:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646204</guid><dc:creator>skeet</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646204</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646204</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-2-self-referential-generic-types.aspx"&gt;part 2&lt;/a&gt; of this series we saw how the message and builder interfaces were self-referential in order to allow the implementation types to be part of the API. That&amp;#39;s one sort of relationship, but in this post we&amp;#39;ll see how the two interfaces relate to each other. If you remember from &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx"&gt;part 1&lt;/a&gt; every generated message type has a corresponding builder type. As it happens, this is implemented with a nested type, so if you had a &lt;code&gt;Person&lt;/code&gt; message, the generated types would be &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; (in a specified namespace, of course).&lt;/p&gt; &lt;p&gt;Without any interfaces involved, this would be very simple. The types would just look like this (with more members, of course):&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;You may well be wondering why there are two methods for creating a builder. The static method is convenient for code which knows it&amp;#39;s dealing with the &lt;code&gt;Person&lt;/code&gt; message. The instance method ends up being part of the message interface, which makes it useful for code which can work with any message. In addition, the constructor for &lt;code&gt;Person.Builder&lt;/code&gt; is accessible in the C# version. In the original Java code the only way of creating a builder is via the methods in the message class; I decided to remove this restriction for the sake of making the oh-so-readable object initializer syntax available in C# 3.&lt;/p&gt; &lt;h3&gt;Redesigning the interfaces to refer to each other&lt;/h3&gt; &lt;p&gt;In part 2 we created self-referential interfaces for the message and builder interfaces which looked like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The constraints on the type parameters allow us to make the API very specific, and we can use the same trick again when we relate the builder and message types together. The step where we introduce a new type parameter to each of them is straightforward:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Unfortunately without any restrictions on the &amp;quot;foreign&amp;quot; type parameter in each interface, we don&amp;#39;t get enough information to make everything work. We need to tie the two types together more tightly, like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;To make this concrete for &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; we end up with implementations like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person : IMessage&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder : IBuilder&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This works, but it&amp;#39;s really ugly. Any generic methods wanting to take a &lt;code&gt;TMessage&lt;/code&gt; type parameter implementing &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt; have to also have a &lt;code&gt;TBuilder&lt;/code&gt; type parameter, and the two constraints need to be expressed each time. It&amp;#39;s a real pain. In fact, I&amp;#39;ve got an &lt;code&gt;IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt; interface which contains almost nothing in it (and which the more generic interface extends). This allows me to get hold of the message type (and use it in the API), inferring the builder type by reflection. That&amp;#39;s a pain too, frankly. It&amp;#39;s a particular nuisance because when I &lt;i&gt;do&lt;/i&gt; infer the builder type, I haven&amp;#39;t actually got any compile-time constraint the lets any other code know that it&amp;#39;s the right builder type for the message type. In one specific case it&amp;#39;s led to this horrific method (in a type generic in &lt;code&gt;TMessage&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;Fortunately this is hidden from public view - and the only reason to do it at all is to enable a pleasant API of &lt;code&gt;MessageStreamIterator&amp;lt;TMessage&amp;gt; : IEnumerable&amp;lt;TMessage&amp;gt; where TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt;. The result of the evil method above is exactly what the caller is likely to want, otherwise I wouldn&amp;#39;t put up with it. However, that sort of excuse has been coming up far too much in the PB implementation, so I&amp;#39;ve had a quick think about what could be done about it.&lt;/p&gt; &lt;h3&gt;Contemplating a more expressive language&lt;/h3&gt; &lt;p&gt;I should really prefix this section by saying that I&amp;#39;m not actually &lt;em&gt;suggesting&lt;/em&gt; this as a way forward for C# or .NET. (I suspect it would take more work in the CLR as well as just in the language; I don&amp;#39;t know enough about CLR generics to say for sure, but I&amp;#39;d be surprised if this were feasible.) I haven&amp;#39;t encountered many situations where I&amp;#39;ve wanted anything like this, and the extra complexity in the language would be quite high, I suspect. Suppose an interface could contain extra type parameters, including constraints, in the body of the interface:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Attention"&gt;// Purely imaginary syntax!&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;, TBuilder.TMessage : TMessage&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TBuilder&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;, TMessage.TBuilder : TBuilder&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TMessage&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;There are various ways in which the interface implementation could indicate the type of TBuilder. The syntax itself isn&amp;#39;t particularly interesting - it&amp;#39;s the extra information which is conveyed which is the important bit. I&amp;#39;ve dithered between this being a step forward and it not. At first glance it looks no better than having both type parameters in the interface declaration, but I believe it would genuinely make a difference. For instance, the above evil method could be written as:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl(Func&amp;lt;TMessage.TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TMessage.TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This time there&amp;#39;s no need for the method to be generic, because the type is already generic in the message type. Furthermore, we can &lt;i&gt;call&lt;/i&gt; this method with no reflection. All other APIs which have previously had to be specify two type parameters can now just specify the one. Apart from anything else, this leaves more scope for type inference in generic methods - passing &lt;i&gt;either&lt;/i&gt; a message &lt;i&gt;or&lt;/i&gt; a builder to a generic method happens occasionally, but it&amp;#39;s very rare to pass in both.&lt;/p&gt; &lt;p&gt;We&amp;#39;ve essentially expressed the relationship between the message type and the builder type a little more explicitly, so that we can guarantee it exists (and use it) at compile time. That&amp;#39;s at the heart of the problem to start with - without a second type parameter in the initial interface declaration, in the current language there&amp;#39;s no way of expressing a close relationship with another type. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I don&amp;#39;t think it would be fair to say that C# really lets us down here - it happens not to support a pretty rare scenario, and that&amp;#39;s fair enough. I&amp;#39;d be interested to know whether any other languages allow the same sort of concepts to be expressed more pleasantly. The ugly solution I&amp;#39;ve presented here does at least work, and it&amp;#39;s nearly invisible to most users, who are likely to just reference the concrete generated types. I&amp;#39;m not happy with the verbosity which has become necessary in many places, but it&amp;#39;s in a good cause. It&amp;#39;s interesting to note that the Java API doesn&amp;#39;t use this sort of doubly-generic relationship: again, covariant return types allow the concrete message and builder types to express their APIs directly and still implement a more general interface at the same time.&lt;/p&gt; &lt;p&gt;In the next part I&amp;#39;ll look at another possibility which would make interfaces and generics a more powerful combination: static interface methods.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646204" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item></channel></rss>