<?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>Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx</link><description>We all know that generics are good - they promote code reuse, static type checking by the compiler, increase runtime performance, allow more flexible OOP designs, lay the foundation for LINQ, help the IDE to provide more helpful IntelliSense and have</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8959958</link><pubDate>Sat, 20 Sep 2008 17:24:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8959958</guid><dc:creator>RednaxelaFX</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I saw this post on VS2008's start page...&lt;/p&gt;
&lt;p&gt;Thought this might serve as another good example why value types should be made immutable. The compiler did nothing wrong, it just faithfully infered the type out. The code wouldn't have work if the 'var' is replaced with its actual type anyway, and we all know that, right?&lt;/p&gt;
&lt;p&gt;Maybe a better solution would be getting rid of mutable value types in the design, if possible. That's much less error-prone.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8960352</link><pubDate>Sun, 21 Sep 2008 06:27:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8960352</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Yes, mutable structs are evil. However the requirements for our object deserializer are such that it has to be universal and be able to deserialize both classes and structs.&lt;/p&gt;
&lt;p&gt;To deserialize an immutable struct, we would need to know about a constructor and actually create the value, not just set properties.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8961194</link><pubDate>Mon, 22 Sep 2008 17:02:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961194</guid><dc:creator>gps</dc:creator><description>&lt;p&gt;You wrote &amp;quot;We all know that generics are good...&amp;quot;. &amp;nbsp;I started with basic, worked my way to C, C++, and Java, then VB (I know, really obleque turn), before I got to C#. &amp;nbsp;While I agree in part that Generics are good, I don't believe var is a good thing at all. &amp;nbsp;It's an evil little thing that says I don't know what I'm working with, so I'll blindly go forward. &amp;nbsp;The C family of languages is strongly typed to aviod such dangerous ideas. &amp;nbsp;Since everything in C# is derived from the Object class, even value types, there should be no reason to use var outside of being assigned an anonymous type, which can only be used safely in the function it is declared.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8961241</link><pubDate>Mon, 22 Sep 2008 17:37:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961241</guid><dc:creator>Matthew</dc:creator><description>&lt;p&gt;Why were you using a struct in the first place?&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8961513</link><pubDate>Mon, 22 Sep 2008 22:32:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961513</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;gps: I don't quite agree about the var part. Var preserves the static type of an expression without explicitly repeating it, thus avoiding redundance. Most functional languages use it in this or another form (&amp;quot;type inference&amp;quot;). There are trade-offs, yes (mostly around readability), but I don't see var being technically harmful. I use it in about 50% of cases and use personal judgement every time. I recommend reading Jon Skeet's &amp;quot;C# in Depth&amp;quot; if you want to learn more about this.&lt;/p&gt;
&lt;p&gt;Matthew: We have structs in code which we don't want to rewrite now. However it was being serialized manually and I was converting it to automatic serialization. That's why I needed the serializer to work with existing code.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8961569</link><pubDate>Tue, 23 Sep 2008 00:08:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961569</guid><dc:creator>Kevin</dc:creator><description>&lt;p&gt;var/type inference has its place in c#, e.g. linq. but that doesn't mean we should abuse it. &lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8961979</link><pubDate>Tue, 23 Sep 2008 09:54:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961979</guid><dc:creator>Jose Luis Chavez del Cid</dc:creator><description>&lt;p&gt;If you know it's a struct, your designed it as struct you know you have to pass the parameters as ByRef (VB) / ref (C#). Theres no mistake there. Three letters makes the difference and you won't have any problem, even if you pass an object.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8962316</link><pubDate>Tue, 23 Sep 2008 17:51:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962316</guid><dc:creator>kevinpilch-bisson</dc:creator><description>&lt;p&gt;And that's exactly why I hate mutable structs so much. &amp;nbsp;See my post on enforcing immutability at &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/kevinpilchbisson/archive/2007/11/20/enforcing-immutability-in-code.aspx"&gt;http://blogs.msdn.com/kevinpilchbisson/archive/2007/11/20/enforcing-immutability-in-code.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8962662</link><pubDate>Tue, 23 Sep 2008 22:55:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962662</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Luis,&lt;/p&gt;
&lt;p&gt;Passing the argument to SetProperty by reference is not appropriate, because it implies that after the method call the reference passed in could be pointing to a completely difference instance! Which is clearly not what the method is trying to accomplish.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8962885</link><pubDate>Wed, 24 Sep 2008 02:53:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962885</guid><dc:creator>Robert Foster</dc:creator><description>&lt;p&gt;@ Luis,&lt;/p&gt;
&lt;p&gt;The whole point is that you don't know in advance that you have a struct, or an object... Kirill has stated that this example has been pulled from an automatic serializer that he is working on... in that case, you need to be able to to pass it various objects without actually knowing what they are.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8963431</link><pubDate>Wed, 24 Sep 2008 12:49:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8963431</guid><dc:creator>Giraffe</dc:creator><description>&lt;p&gt;Another option would be to force structs to be passed by reference by using generics:&lt;/p&gt;
&lt;p&gt;static void SetProperty&amp;lt;T&amp;gt;(T f) where T : class&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			Type type = f.GetType();&lt;/p&gt;
&lt;p&gt;			PropertyInfo property = type.GetProperty(&amp;quot;Bar&amp;quot;);&lt;/p&gt;
&lt;p&gt;			property.SetValue(f, 1, new object[0]);&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		static void SetProperty&amp;lt;T&amp;gt;(ref T f) where T : struct&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			object fObj = (object)f;&lt;/p&gt;
&lt;p&gt;			SetProperty(fObj);&lt;/p&gt;
&lt;p&gt;			f = (T)fObj;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;This will throw a compile error if you call SetProperty with a value type without specifying it as a ref parameter. &amp;nbsp; Obviously, this won't work if the calls should be the same for object and value types:&lt;/p&gt;
&lt;p&gt;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			var f1 = new Foo();&lt;/p&gt;
&lt;p&gt;			SetProperty(ref f1);&lt;/p&gt;
&lt;p&gt;			Foo foo1 = (Foo)f1;&lt;/p&gt;
&lt;p&gt;			Console.WriteLine(foo1.Bar);&lt;/p&gt;
&lt;p&gt;			object f2 = new Foo();&lt;/p&gt;
&lt;p&gt;			SetProperty(f2);&lt;/p&gt;
&lt;p&gt;			Foo foo2 = (Foo)f2;&lt;/p&gt;
&lt;p&gt;			Console.WriteLine(foo2.Bar);&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8966986</link><pubDate>Fri, 26 Sep 2008 23:07:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8966986</guid><dc:creator>Harper Shelby</dc:creator><description>&lt;p&gt;@Kirill&lt;/p&gt;
&lt;p&gt; Certainly var has its place as a 'type' name for variables holding the return values of LINQ queries that will be anonymous types generated by the compiler, but in situations where the programmer knows the type in advance, it seems like it invites issues like this. I would personally recommend coding standards that explicitly forbade its use in those situations for that reason.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8967680</link><pubDate>Sat, 27 Sep 2008 21:10:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8967680</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Well, as I said, I've never hit this case before, so I thought var can do no harm. I used to think carefully everytime I needed to declare a local variable, and I now I will think even more carefully. But I still love var and I expect myself using it in the future as well where appropriate (I'll just have to be more careful). Not necessarily for anonymous types (which I rather almost never use), but also where it increases readability and the type is clear from the variable name/ambience.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8969869</link><pubDate>Tue, 30 Sep 2008 08:32:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8969869</guid><dc:creator>Bruce Pierson</dc:creator><description>&lt;p&gt;&amp;quot;To deserialize an immutable struct, we would need to know about a constructor and actually create the value, not just set properties.&amp;quot;&lt;/p&gt;
&lt;p&gt;I don't understand this. If you are using reflection, can you not still deserialize a struct by setting the fields rather than the properties? After all, the framework somehow knows how to deserialize &amp;quot;immutable&amp;quot; value types... Since inheritance is not an issue, you know all the fields in a value type will be DeclaredOnly, and there will always be a parameterless default constructor suitable for Activator.CreateInstance().&lt;/p&gt;
&lt;p&gt;At the very least, if you want to use properties and make it immutable by normal means, just use a &amp;quot;private set&amp;quot; and then let reflection find that.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8969888</link><pubDate>Tue, 30 Sep 2008 08:53:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8969888</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;Hi Bruce,&lt;/p&gt;
&lt;p&gt;all your comments are very valid. From a couple of hints I see that you clearly know what you're talking about. However we have a couple of requirements:&lt;/p&gt;
&lt;p&gt;1. We want to keep our serializer/deserializer very simple, maintainable (500 lines of code for deserializer and 150 lines of code for serializer) and keep full control over it&lt;/p&gt;
&lt;p&gt;2. We only serialize public writable instance properties, we don't even look at fields&lt;/p&gt;
&lt;p&gt;3. The list of participating properties is returned by a piece of common reflection logic that we want to keep really simple/trivial&lt;/p&gt;
&lt;p&gt;4. This is not shipping code, so we just want to get it working and move on - my solution turned out to be the best in terms of cost/benefit - quick, maintainable and does the job.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8970715</link><pubDate>Wed, 01 Oct 2008 00:00:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8970715</guid><dc:creator>Bruce Pierson</dc:creator><description>&lt;p&gt;Sure, that makes sense now. I just wanted to make sure I wasn't missing something in my own mapper / deserializer code. Thanks for the reinforcement.&lt;/p&gt;
&lt;p&gt;I've been bitten by the value-type bug before, so that's why I viewed this in the first place. Good info, thanks.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8971023</link><pubDate>Wed, 01 Oct 2008 05:16:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8971023</guid><dc:creator>Stas Neverov</dc:creator><description>&lt;p&gt;Var is good! It reduces the &amp;quot;noise&amp;quot; in your code (especially when you use generics a lot) thus making it more readable. For me readability is much more important then this potential stupid simple issues when you mix up var with object.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8972229</link><pubDate>Thu, 02 Oct 2008 00:30:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8972229</guid><dc:creator>Justin Chase</dc:creator><description>&lt;p&gt;You probably should pass the struct by ref rather than cast into an object. You are probably going to see some performance gains if you do it that way, not to mention the fact that this is uuuuuugly.&lt;/p&gt;
&lt;p&gt;static void SetProperty(ref object obj)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; //...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;var f = new Foo();&lt;/p&gt;
&lt;p&gt;SetProperty(ref f);&lt;/p&gt;
&lt;p&gt;@David Nelson&lt;/p&gt;
&lt;p&gt;I believe you are confusing 'ref' with 'out'.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8974061</link><pubDate>Thu, 02 Oct 2008 21:09:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974061</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Justin,&lt;/p&gt;
&lt;p&gt;No, I am not confusing them. 'out' is merely a specialized form of 'ref', in which the argument does not have to be initialized before being passed in (as opposed to 'ref' in which the argument does have to be initialized before being passed in). In fact the CLR does not even support 'out' parameters; the C# compiler simply implements them as ref parameters in IL (try calling a method with an out parameter that was created in C# from a VB.NET application).&lt;/p&gt;
&lt;p&gt;In both cases, the called method can set the reference to point to a different instance than what was passed in. The only difference is that an 'out' parameter is required to be set by the called method, since it may not have been initialized before the method was called; whereas setting a 'ref' parameter is optional, because it was required to already have a value before it was passed.&lt;/p&gt;
</description></item><item><title>const ref</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#8974315</link><pubDate>Thu, 02 Oct 2008 23:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974315</guid><dc:creator>Gary W.</dc:creator><description>&lt;p&gt;I looked up &amp;quot;const ref&amp;quot; because that seems like what what is being desired by some in the above comments, and it appears that this doesn't exist in C# (like in C++). &amp;nbsp;I had to look this up to get a better understanding.&lt;/p&gt;
&lt;p&gt;I found this link helpful:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://channel9.msdn.com/forums/Coffeehouse/255508-const-ref-in-C/"&gt;http://channel9.msdn.com/forums/Coffeehouse/255508-const-ref-in-C/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(I need to read more about &amp;quot;var&amp;quot; as well....)&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#9001680</link><pubDate>Thu, 16 Oct 2008 13:39:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9001680</guid><dc:creator>James Hart</dc:creator><description>&lt;p&gt;I don't think you're being fair to 'var' here, since the problems are still there with an explicitly typed variable. If anything's caused this problem, it's in the nature of C#'s handling of automatically boxing value types when they're passed to a method that accepts System.Object (or an interface, for that matter).&lt;/p&gt;
&lt;p&gt;When a method says it accepts a System.Object as an argument, it seems natural for the method to proceed on the assumption that the calling code also has a reference to the object passed. But if the argument passed in is a value type, C# boxes it up into a reference, passes the reference in, but /discards the reference/ from the perspective of the calling method. That's the bit that catches the called method out - all its work on the object its given is going to be thrown away.&lt;/p&gt;
&lt;p&gt;It's as if&lt;/p&gt;
&lt;p&gt;Foo f = new Foo();&lt;/p&gt;
&lt;p&gt;SetProperty(f);&lt;/p&gt;
&lt;p&gt;Console.WriteLine(f.Bar);&lt;/p&gt;
&lt;p&gt;is actually treated as&lt;/p&gt;
&lt;p&gt;Foo f = new Foo();&lt;/p&gt;
&lt;p&gt;object temp = f;&lt;/p&gt;
&lt;p&gt;SetProperty(temp);&lt;/p&gt;
&lt;p&gt;Console.WriteLine(f.Bar);&lt;/p&gt;
&lt;p&gt;It seems it would be more intuitive if what C# actually did was this:&lt;/p&gt;
&lt;p&gt;Foo f = new Foo();&lt;/p&gt;
&lt;p&gt;object temp = f;&lt;/p&gt;
&lt;p&gt;SetProperty(temp);&lt;/p&gt;
&lt;p&gt;f = (Foo) temp;&lt;/p&gt;
&lt;p&gt;Console.WriteLine(f.Bar);&lt;/p&gt;
&lt;p&gt;But I guess that could get messy if SetProperty were to hand a reference to temp off to another thread...&lt;/p&gt;
&lt;p&gt;As it is, you can of course do that yourself explicitly, which is pretty much what your code ends up doing by declaring f to be of type object in the first place.&lt;/p&gt;
</description></item><item><title>re: Too much type information, or welcome back System.Object and boxing</title><link>http://blogs.msdn.com/kirillosenkov/archive/2008/09/19/too-much-type-information-or-welcome-back-system-object-and-boxing.aspx#9013661</link><pubDate>Thu, 23 Oct 2008 23:45:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9013661</guid><dc:creator>Alex Fedin</dc:creator><description>&lt;p&gt;I would like to ask another question: why did you even choose to have your own serializer, if there are a number of them already? (Xml Serializer, Xaml Serializer, Soap Formatter, WCF Formatters).&lt;/p&gt;
&lt;p&gt;Was the major intention &amp;quot;to reinvent the wheel&amp;quot;?&lt;/p&gt;
</description></item></channel></rss>