<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx</link><description>First off, a little explanation for the post title: not that I didn't understand generics before. I'd had a pretty good understanding of how they work, how they are implemented in the CLR, what facilities are provided by the compiler, etc. It's just that</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8879036</link><pubDate>Tue, 19 Aug 2008 18:40:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8879036</guid><dc:creator>Robin</dc:creator><description>&lt;p&gt;That little bit about the typed exceptions is really cool. &amp;nbsp;I already have ideas as to how to use this.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8879174</link><pubDate>Tue, 19 Aug 2008 19:38:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8879174</guid><dc:creator>Jacob</dc:creator><description>&lt;p&gt;Have you considered returning InvalidOperationException&amp;lt;TCommand&amp;gt; (rather than void) from the extension method? Then your callsites become:&lt;/p&gt;
&lt;p&gt;&amp;quot;throw this.NewInvalidOperationException(...);&amp;quot;&lt;/p&gt;
&lt;p&gt;The nice thing with that pattern is that the debugger callstack ends up in the right place (on break when thrown) rather than one method too deep.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8879385</link><pubDate>Tue, 19 Aug 2008 20:54:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8879385</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Jacob - that sounds like a good idea :)&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8909761</link><pubDate>Sat, 30 Aug 2008 16:46:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8909761</guid><dc:creator>Balaji</dc:creator><description>&lt;p&gt;A nice post kirill... Never knew of such &amp;quot;Typed&amp;quot; Exceptions :-)&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8934820</link><pubDate>Mon, 08 Sep 2008 23:56:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8934820</guid><dc:creator>Darren Clark</dc:creator><description>&lt;p&gt;In the &amp;quot;interesting usage of generics&amp;quot; thread I'll add this pattern:&lt;/p&gt;
&lt;p&gt;Note that this is a made up example and I know there's better ways to do what it does, but it's just an example. The pattern is useful whenever you have a generic factory class, not just XmlSerializer.&lt;/p&gt;
&lt;p&gt;public class MyBase&amp;lt;T&amp;gt; where T:MyBase&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; private static XmlSerializer serializer = new XmlSerializer(typeof(T));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected virtual AfterDeserialization()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; } &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; public static T CreateFromXml(Stream stream)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; T newObj = (T) serializer.Deserialize(stream);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; newObj.AfterDeserialization();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; return newObj;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Basically the idea is that you can have a single generic base class implement a factory pattern for multiple derived classes. &lt;/p&gt;
&lt;p&gt;It also points out another interesting thing about generics. Static members on a generic class are PER T. &lt;/p&gt;
&lt;p&gt;Using a pattern like this you have to make sure you understand what that means.&lt;/p&gt;
&lt;p&gt;You CANNOT properly do &lt;/p&gt;
&lt;p&gt;MyDerivedA: MyBase&amp;lt;MyDerivedA&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and &lt;/p&gt;
&lt;p&gt;MyDerivedB: MyDerivedA&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Since the call to MyDerivedB.Deserialize will use the same serializer as MyDerivedA, resulting in a typecast error.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8936389</link><pubDate>Tue, 09 Sep 2008 14:17:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8936389</guid><dc:creator>madduck</dc:creator><description>&lt;p&gt;How does 'class MyGenericType&amp;lt;T&amp;gt; : T' lead to multiple inheritance?&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8936421</link><pubDate>Tue, 09 Sep 2008 14:47:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8936421</guid><dc:creator>Wiebe</dc:creator><description>&lt;p&gt;Hi Kirill,&lt;/p&gt;
&lt;p&gt;thank you for the article, but I fail to see what this way of using generics improves.&lt;/p&gt;
&lt;p&gt;Why not just add the ThrowInvalidOperationException method to the Command class, and instantiate the exception with this.GetType()? That's just as safe...&lt;/p&gt;
&lt;p&gt;Marking it virtual would even allow you to override the behavior in derived commands where the extension/static method approach doesn't...&lt;/p&gt;
&lt;p&gt;Additionally, I don't see the advantage of adding extension/(static) methods to classes that you have created yourself, or am I missing the point here?&lt;/p&gt;
&lt;p&gt;Sorry I don't mean to be negative but I think you've found a complex solution to a simple problem :)&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8937344</link><pubDate>Tue, 09 Sep 2008 22:59:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8937344</guid><dc:creator>Darren Clark</dc:creator><description>&lt;p&gt;The advantage of using extension methods there is that the &amp;quot;this&amp;quot; is the most derived type when it is used.&lt;/p&gt;
&lt;p&gt;Funny enough you could use the pattern I outlined above as well.&lt;/p&gt;
&lt;p&gt;Where you have Command&amp;lt;T&amp;gt; where T:Command&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;Then you have CopyCommand: Command&amp;lt;CopyCommand&amp;gt; etc.&lt;/p&gt;
&lt;p&gt;and finally void ThrowInvalidOperationException&amp;lt;T&amp;gt;(message)&lt;/p&gt;
&lt;p&gt;I like the extension method pattern much better though since it elegantly supports more than one layer of derivations. With mine you'd end up having:&lt;/p&gt;
&lt;p&gt;Command&amp;lt;T&amp;gt; where T: Command&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;CopyCommandBase&amp;lt;T&amp;gt;: Command&amp;lt;T&amp;gt; where T:CopyCommandBase&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;CopyCommand: CopyCommandBase&amp;lt;CopyCommand&amp;gt;&lt;/p&gt;
&lt;p&gt;SpecialCopyCommand: CopyCommandBase&amp;lt;SpecialCopyCommand&amp;gt;&lt;/p&gt;
&lt;p&gt;Note you can't directly derive SpecialCopyCommand directly from CopyCommand. If you do then all the T's will reference CopyCommand, NOT the most derived as you wanted.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8938309</link><pubDate>Wed, 10 Sep 2008 04:12:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8938309</guid><dc:creator>Damon Wilder Carr</dc:creator><description>&lt;p&gt;uhh.... &lt;/p&gt;
&lt;p&gt; &amp;nbsp; public abstract class Command&amp;lt;T&amp;gt; : ICommand&amp;lt;Object&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;where T : Command&amp;lt;T&amp;gt;, ICommand&amp;lt;Object&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public virtual void Execute() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new NotImplementedException();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;protected InvalidOperationExceptionTyped GetException(String message) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new InvalidOperationExceptionTyped(this, message);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#region Nested type: InvalidOperationExceptionTyped&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public class InvalidOperationExceptionTyped : InvalidOperationException {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private readonly Command&amp;lt;T&amp;gt; _typeUsed;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public InvalidOperationExceptionTyped(Command&amp;lt;T&amp;gt; typeUsed, string message)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: base(&amp;quot;Type was &amp;quot; + typeUsed + &amp;quot; message was &amp;quot; + message) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_typeUsed = typeUsed;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public Command&amp;lt;T&amp;gt; CommandType {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return _typeUsed; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object ICommand&amp;lt;object&amp;gt;.Instance {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return Instance; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public Command&amp;lt;T&amp;gt; Instance&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;void ICommand&amp;lt;object&amp;gt;.Execute() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Execute();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class CopyCommand : Command&amp;lt;CopyCommand&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void Execute()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw GetException(&amp;quot;Something went wrong&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public abstract class CutCommandBase : Command&amp;lt;CutCommandBase&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public abstract override void Execute();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public abstract class CutCommandShapeBase : CutCommandBase {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public abstract override void Execute();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class CutCommandShapeCircle : CutCommandShapeBase&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void Execute() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw GetException(&amp;quot;THis is a little crazy I admint&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class CutCommandPoint : CutCommandBase&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void Execute()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw GetException(&amp;quot;Something went wrong&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface ICommand&amp;lt;T&amp;gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;T Instance { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;void Execute();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;P.S. This is a joke (grin)..&lt;/p&gt;
&lt;p&gt;damon&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8944719</link><pubDate>Thu, 11 Sep 2008 17:47:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944719</guid><dc:creator>Gareth Watson</dc:creator><description>&lt;p&gt;This was an ace article!&lt;/p&gt;
&lt;p&gt;What this means for us is that our DAL which using interfaces fill objects with data from our datastore. &amp;nbsp;We were using ArrayLists because it was impossible to declare a list of interfaces and then add objects that implement that interface to it.&lt;/p&gt;
&lt;p&gt;The method described above now allows be to get a List&amp;lt;T&amp;gt; passed then I can restrict to making sure that T implements a certain interface. Then &amp;nbsp;create new T's and add them to the list!&lt;/p&gt;
&lt;p&gt;Thanks very much for this! &amp;nbsp;It's going to save us loads of code and will probably be more efficient too. Win-Win not often you get that.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8944847</link><pubDate>Thu, 11 Sep 2008 19:28:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944847</guid><dc:creator>Alec</dc:creator><description>&lt;p&gt;So what is the difference between these two?&lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; : IEquatable&amp;lt;T&amp;gt; and&lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; where T: IEquatable&amp;lt;T&amp;gt;&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8945218</link><pubDate>Thu, 11 Sep 2008 22:01:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8945218</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;Alec,&lt;/p&gt;
&lt;p&gt;In the first definition, MyType&amp;lt;T&amp;gt; implements IEquatable&amp;lt;T&amp;gt;. In the second MyType&amp;lt;T&amp;gt; does not implement IEquatable at all; rather it requires that T implement IEquatable&amp;lt;T&amp;gt;, presumably so that it MyType&amp;lt;T&amp;gt; can call members of IEquatable&amp;lt;T&amp;gt; in its definition.&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8952154</link><pubDate>Mon, 15 Sep 2008 09:22:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8952154</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Darren: you essentially constrain the type parameter to be a derived type - that's really cool!&lt;/p&gt;
&lt;p&gt;madduck: I updated the post, thanks.&lt;/p&gt;
&lt;p&gt;Wiebe: this approach improves over the following:&lt;/p&gt;
&lt;p&gt;1. I'm not modifying the Command types at all, the extension method is a mix-in (the base class Command can even be in a separate assembly, and I don't need to change it).&lt;/p&gt;
&lt;p&gt;2. If I instantiate the exception with this.GetType(), I get runtime type information, but not compile-time. In the exception handling logic we're having, it's important to preserve static typing, which GetType() doesn't have.&lt;/p&gt;
&lt;p&gt;3. Marking it virtual would mean more work to do in every derived class.&lt;/p&gt;
&lt;p&gt;4. The advantage of adding extension methods to my own code is better separation of logic and promotes decoupling. Such code is more flexible and I can easily apply extension methods to newly introduced types without doing any extra work in the types themselves. This is what I call orthogonal code, and orthogonal code is pure goodness.&lt;/p&gt;
&lt;p&gt;5. You're not negative, your critique is great and very polite. I agree that my solution might be complex, but this works great for me and my team - I don't force you to use it. This is just a hint of what you can do with the language - maybe you can use a similar trick somewhere else. I used the exceptions example just for an example of the technique.&lt;/p&gt;
&lt;p&gt;Damon: that's hardcore!&lt;/p&gt;
&lt;p&gt;Alec: it helps to think of a container type such as List&amp;lt;T&amp;gt;. The line List&amp;lt;T&amp;gt; : IEquatable&amp;lt;T&amp;gt; imposes the constraint on the container type itself, whereas the line List&amp;lt;T&amp;gt; where T: IEquatable&amp;lt;T&amp;gt; imposes the constraint on the element type in the container.&lt;/p&gt;
&lt;p&gt;Thanks everyone!&lt;/p&gt;
</description></item><item><title>More comments on generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8952178</link><pubDate>Mon, 15 Sep 2008 10:30:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8952178</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Here are some more unsorted thoughts on generics to continue this post (which has some interesting comments&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8957088</link><pubDate>Thu, 18 Sep 2008 13:24:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8957088</guid><dc:creator>Barry Wimlett</dc:creator><description>&lt;p&gt;Quote:&lt;/p&gt;
&lt;p&gt;Let's consider another insight: what's the difference between:&lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; : IEquatable&amp;lt;T&amp;gt; and &lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; where T: IEquatable&amp;lt;T&amp;gt; &lt;/p&gt;
&lt;p&gt;If you immediately grasped what's going on, then good. Because it takes me a while to understand what's going on. Thinking about this difference made another aspect of generics clearer for me: with generics, there are always at least two different types involved: the actual type and the generic type parameters.&lt;/p&gt;
&lt;p&gt;&amp;lt;/Quote&amp;gt;&lt;/p&gt;
&lt;p&gt;The thing is I din't grasp what is going on, and you failed to explain it, you just ploughed on with the next example.&lt;/p&gt;
&lt;p&gt;Ay chance you could explain further so the rest of us can catch up ?&lt;/p&gt;
</description></item><item><title>re: How I started to really understand generics</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/08/19/how-i-started-to-really-understand-generics.aspx#8958320</link><pubDate>Fri, 19 Sep 2008 09:53:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958320</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Alec, Barry,&lt;/p&gt;
&lt;p&gt;I've updated the post with some more explanations regarding &lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; : IEquatable&amp;lt;T&amp;gt; and &lt;/p&gt;
&lt;p&gt;class MyType&amp;lt;T&amp;gt; where T: IEquatable&amp;lt;T&amp;gt; &lt;/p&gt;
&lt;p&gt;Please don't take those examples as real-life - they are very contrived and only serve the purpose of explaining stuff about generics, type parameters and constraints.&lt;/p&gt;
&lt;p&gt;Feel free to ask more questions.&lt;/p&gt;
</description></item></channel></rss>