<?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>C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx</link><description>The fact that a struct can implement an interface is well known and so is the fact that casting a value type into an interface leads to boxing of the value type. This is because methods in interfaces are defined as virtual and to resolve virtual references,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#477344</link><pubDate>Wed, 05 Oct 2005 17:55:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:477344</guid><dc:creator>Rick Scott</dc:creator><description>That does make perfect sense, but only after some thought. A struct implementing an interface is, to say the least, something one should approach with great caution. Perhaps a compiler warning is appropriate?</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#477469</link><pubDate>Wed, 05 Oct 2005 22:37:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:477469</guid><dc:creator>kfarmer</dc:creator><description>I recall reading somewhere that structs should not be used for mutable types.  You could consider them as points in a space consisting of many values.  &lt;br&gt;&lt;br&gt;promote(), really, is as much a mutation as String.Trim(), and should return a different point in the employee space:  one that represents the employee after promotion.</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#477782</link><pubDate>Thu, 06 Oct 2005 15:34:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:477782</guid><dc:creator>James Arendt</dc:creator><description>As kfarmer stated, the example violates the design that structs should be immutable value-types. Yes, boxing is going on here, but the code would still have the same issue if you took out boxing and simply assigned the employee object to another variable.&lt;br&gt;&lt;br&gt;Employee employee = new Employee(&amp;quot;Cool Guy&amp;quot;, 65);&lt;br&gt;Employee employee2 = employee;&lt;br&gt;Console.WriteLine(employee2);&lt;br&gt;employee.promote();&lt;br&gt;Console.WriteLine(employee2);&lt;br&gt;&lt;br&gt;Structs are value-types. They should be treated as such. That's the more important lesson to take from this example.</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#477796</link><pubDate>Thu, 06 Oct 2005 16:43:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:477796</guid><dc:creator>Andrei Ignat</dc:creator><description>With lots a feature  - the language becomes more and more difficult to understand - and for NO GOOD REASON.&lt;br&gt;It was REALLY necessary that a struct can implement an interface? </description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#478208</link><pubDate>Fri, 07 Oct 2005 16:33:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:478208</guid><dc:creator>James Arendt</dc:creator><description>Andrei, I would argue that the problem is not that a struct can implement an interface, but that the language designers do not enforce immutability for structs. Allowing structs to implement interfaces simplifies things by allowing developers to treat all types in their program more uniformly.</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#6819187</link><pubDate>Thu, 20 Dec 2007 20:35:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6819187</guid><dc:creator>Raj</dc:creator><description>&lt;p&gt;One important factor to consider is time.&lt;/p&gt;
&lt;p&gt;To allocate an array of 1000 structs is a lot quicker (as one doesnt have to go through initializing each and every struct object). Where as if one had to allocate a 1000 class objects - that would take a lot more time.&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#8327409</link><pubDate>Thu, 20 Mar 2008 17:05:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8327409</guid><dc:creator>Jeroen</dc:creator><description>&lt;p&gt;As James correctly states, the problem would be the same if you used two instances of the struct. The problem is actually just the opposite: the struct will behave like a class if you cast it to the interface:&lt;/p&gt;
&lt;p&gt;IPromotion employee = new Employee(&amp;quot;Cool Guy&amp;quot;, 65);&lt;/p&gt;
&lt;p&gt;IPromotion employee2 = employee;&lt;/p&gt;
&lt;p&gt;Console.WriteLine(employee2); // outputs 65&lt;/p&gt;
&lt;p&gt;employee.promote();&lt;/p&gt;
&lt;p&gt;Console.WriteLine(employee2); // outputs 66!!&lt;/p&gt;
&lt;p&gt;The copy from employee to employee2 now only copies the *reference* to the (already boxed) instance, so both references reference the *same* value.&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#8397577</link><pubDate>Tue, 15 Apr 2008 16:50:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8397577</guid><dc:creator>andarno</dc:creator><description>&lt;p&gt;Great post, Abhinaba. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;And thanks, Jeroen, for suggesting the workaround. &amp;nbsp;It actually works in my case to rearrange the lines a bit as you have to get around my problem.&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#8591285</link><pubDate>Wed, 11 Jun 2008 15:58:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8591285</guid><dc:creator>Jeroen</dc:creator><description>&lt;p&gt;Andarno,&lt;/p&gt;
&lt;p&gt;I did not quite mean it as a workaround, in fact I posted it to point out a problem area in working with interfaces and structs. If you want your thingamabob to act like a class, then you should m&amp;#225;ke it a class, and not make a struct and box it.&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9181666</link><pubDate>Sun, 07 Dec 2008 06:50:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9181666</guid><dc:creator>Scott Youngblut</dc:creator><description>&lt;p&gt;The main reason to me, that you would most certainly want to make structs implement interfaces is to get the added advantage of generic parameter type constraints.&lt;/p&gt;
&lt;p&gt;The available options you have for type constraints are struct,class,new(), (Interfaces), and (abstract classes).&lt;/p&gt;
&lt;p&gt;To get better compile time checking you may want the method signature to look like this&lt;/p&gt;
&lt;p&gt;public static void Add&amp;lt;TEnum&amp;gt;(TEnum augend, TEnum addend)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; where TEnum : struct, IEnum&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return augend.Add(addend);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Thus the caller is not allowed to pass in an integer as the most basic type constraint of just &amp;quot;struct&amp;quot; would allow.&lt;/p&gt;
&lt;p&gt;Interfaces to the rescue?&lt;/p&gt;
&lt;p&gt;thoughts?&lt;/p&gt;
</description></item><item><title>ValueType | hilpers</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9347832</link><pubDate>Tue, 20 Jan 2009 18:18:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9347832</guid><dc:creator>ValueType | hilpers</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.hilpers.com/305229-valuetype"&gt;http://www.hilpers.com/305229-valuetype&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Modyfing value types through an interface doesn't work with generics (Brain Puzzle?) | keyongtech</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9362811</link><pubDate>Thu, 22 Jan 2009 07:43:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9362811</guid><dc:creator>Modyfing value types through an interface doesn't work with generics (Brain Puzzle?) | keyongtech</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.keyongtech.com/658785-modyfing-value-types-through-an"&gt;http://www.keyongtech.com/658785-modyfing-value-types-through-an&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Pop Quiz: Applied Object-Oriented Principles with C# | Necessary and Sufficient</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9454231</link><pubDate>Mon, 02 Mar 2009 15:21:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9454231</guid><dc:creator>Pop Quiz: Applied Object-Oriented Principles with C# | Necessary and Sufficient</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.necessaryandsufficient.net/2009/02/pop-quiz-applied-object-oriented-principles-with-c/"&gt;http://www.necessaryandsufficient.net/2009/02/pop-quiz-applied-object-oriented-principles-with-c/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9573866</link><pubDate>Tue, 28 Apr 2009 20:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9573866</guid><dc:creator>Zhi Wen</dc:creator><description>&lt;p&gt;nice post, I need some kind of polymorphism to store many different kind of structs in a single list and found your post. &amp;nbsp;I don't need to modify the struct so interface will work perfectly for me. &amp;nbsp;Thanks.&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9573922</link><pubDate>Tue, 28 Apr 2009 20:57:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9573922</guid><dc:creator>abhinaba</dc:creator><description>&lt;p&gt;Zhi are you sure that your approach is not going to be a perf pitfall? Ensure that boxing doesn't become a blocker for you&lt;/p&gt;
</description></item><item><title>re: C#: structs and Interface </title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx#9641915</link><pubDate>Tue, 26 May 2009 21:41:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9641915</guid><dc:creator>Zhi Wen</dc:creator><description>&lt;p&gt;Just did some more reading. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;I need some kind of polymorphism to store many &amp;gt;&amp;gt;different kind of structs in a single list and found &amp;gt;&amp;gt;your post. &amp;nbsp;I don't need to modify the struct so &amp;gt;&amp;gt;interface will work perfectly for me.&lt;/p&gt;
&lt;p&gt;Actually, this is probably a bad idea. &amp;nbsp;It will probably store each struct as a reference type on heap. &amp;nbsp;I am not sure. &amp;nbsp;This whole thing is just confusing.&lt;/p&gt;
</description></item></channel></rss>