<?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>GrantRi's WebLog [MS] : C# Language</title><link>http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx</link><description>Tags: C# Language</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Not-so-new C# Compiler Features</title><link>http://blogs.msdn.com/grantri/archive/2005/03/17/398086.aspx</link><pubDate>Thu, 17 Mar 2005 18:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:398086</guid><dc:creator>grantri</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/grantri/comments/398086.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=398086</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=398086</wfw:comment><description>&lt;p&gt;So a while back somebody asked what new compiler features were coming out for Whidbey that weren't part of new language features.&amp;nbsp; Well you've already heard about &lt;A href="http://blogs.msdn.com/somasegar/archive/2004/10/15/242853.aspx"&gt;Edit and Continue&lt;/a&gt;.&amp;nbsp; There's also the really cool &lt;a href="http://msdn.microsoft.com/vcsharp/default.aspx?pull=/library/en-us/dnvs05/html/vs05_refac.asp"&gt;Refactoring&lt;/a&gt; built into the IDE and built off of the compiler's source-code analysis.&amp;nbsp; As an attempt to improve the debugging experience for those of you with 500+ default.aspx files in your solution, they've added some hashes to the PDB files and improved file lookups to the debugger.&amp;nbsp; Partly to improve E&amp;amp;C but also to improve the overall debugging experience, the compiler has done some 'de-optimization' to the code (basically adding NOPs and storing more temporaries to locals).&amp;nbsp; Don't worry none of this should impact the performance of your relase code as long as you remember to use &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscomp/html/vcrefUEnabledisableOptimizations.asp"&gt;/optimize+&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;This does bring me to another question that somebody asked: what optimizations does the C# compiler do?&amp;nbsp; The answer is very few.&amp;nbsp; Most of them would fall into the category of flow-graph optimizations: remove branches to next, short circuit branches to branches, remove dead code, invert conditionals to eliminate branches around unconditional branches, etc.&amp;nbsp; It also does very basic constant folding, although you can easily thwart this by reordering the math (e.g. "4 + 5 + a" folds to&amp;nbsp; "9 + a", but "4 + a + 5" stays the same).&lt;/p&gt; &lt;p&gt;You might ask why the compielr doesn't do more.&amp;nbsp; Well I can see 2 reasons: if&amp;nbsp;the compiler&amp;nbsp;optimizes too much it will make the JIT work harder to perform its optimizations and any optimization that applies ot the C# compiler should be written in a way that all compilers targeting MSIL could share.&amp;nbsp; For the second reason most people prefer to put the optimization into the JIT.&amp;nbsp; My &lt;u&gt;personal&lt;/u&gt; optinion is that this was a bad choice.&amp;nbsp; Why reoptimize stuff every time the code is run?&amp;nbsp; NGEN helps, but wouldn't be nicer to just have a simple MSIL optimizer that performed classic dragon-book style machine independent optimizations?&lt;/p&gt; &lt;p&gt;Mike Montwill wrote a nice piece about the exact optimizations with real examples &lt;a href="http://msdn.microsoft.com/msdnmag/issues/05/01/COptimizations/default.aspx?side=true#a"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Well enough for this post.&amp;nbsp; I'm still looking for things to write about.&lt;/p&gt; &lt;p&gt;--Grant&lt;/p&gt; &lt;p&gt;&lt;font size="2"&gt;[Edit: Added link to Mike's aritcle]&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=398086" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item><item><title>Some of my opinions on Generics</title><link>http://blogs.msdn.com/grantri/archive/2004/12/31/344967.aspx</link><pubDate>Fri, 31 Dec 2004 22:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:344967</guid><dc:creator>grantri</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/grantri/comments/344967.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=344967</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=344967</wfw:comment><description>&lt;p&gt;&lt;em&gt;Disclaimer: These are all my opinions, so don't take them to mean anything more than the futile thoughts of an insignificant bystander who happened to be fortunate enough to listen to a few of the C# language design meetings and occasionally interact with some of the designers.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;John and a few others have compared the CLR's generics (hereafter referred to as simply 'Generics') to C++'s templates ('Templates'). In some ways I think this is beneficial, but in other ways I think it is like comparing apples and oranges. This is my attempt to compare and contrast them and explain why I think certain comparisons are invalid.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;They both allow polymorphism in a second dimension. In addition to inheritance, you now also have instantiation.&lt;/li&gt; &lt;li&gt;They both facilitate code reuse by allowing the programmer to write type-agnostic algorithms, while in most cases preserving the type-safety and performance of type-specific algorithms.&lt;/li&gt; &lt;li&gt;Templates are a compile-time mechanism, while Generics are instantiated at runtime. Along this vein, I like to think of Templates as compile-time macros on steroids. All the C++ compilers I'm aware of implement Templates entirely in the front-end, very similarly to how the preprocessor implements macros. Because of this templates can be used and abused in many ways that Generics can't.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I think most everybody will agree with me at this point. So the next major question is why did the CLR people decide to go with the 'limited' functionality of Generics instead of Templates? Well for starters, the CLR as a platform does not preclude C++ or any other language from incorporating Templates since that all happens at compile time and require no help from the CLR. Now granted there is no metadata to express templates that would make them cross-language, but as far as I know, there has never been any cross-language Templates.&lt;/p&gt; &lt;p&gt;John stated, "Generics seem to really answer the freshman CS collections problems while missing some of the more expressive power of C++ templates." I agree mostly with his statement. I disagree with the implied statement that Generics don't solve/answer the 'bigger &lt;em&gt;real world&lt;/em&gt; problems that &lt;em&gt;real&lt;/em&gt; programmers face'. First off we must remember from our CS theory classes that all computer languages are functionally equivalent. That is to say if you can do it in one of them, you can do it in all the others, but it might be significantly harder or require more work on the part of the programmer, but it is not impossible. So what are the real situations that real programmers face that Templates make easier but Generics don't (or at least not as easy)? I'm sure there are a few. The biggest one that springs to my mind is constants. I've written a few templates that take as a parameter the initial size or the size to grow. With Generics you're forced to use a readonly field to store such information. Not quite as elegant, but close enough for me until a performance test proves otherwise (in which case I'll probably look at changing algorithms first).&lt;/p&gt; &lt;p&gt;I personally would contend that being able to write type-agnostic collections and algorithms, that are still type-specific to the consumer really is the 90% case. Hence Generics that are really just trying to solve the freshman CS problem are solving most of the real problems. At least for now, I'm sure in a few years that will all change.&lt;/p&gt; &lt;p&gt;BTW John, you and I took many of the same freshman classes, and I don't remember any stuff about collections, type safety, templates or that sort of thing until at least my sophomore or junior year. Are there any colleges out there that teach that sort of stuff to first year CS students?&lt;/p&gt; &lt;p&gt;--Grant&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=344967" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item><item><title>More Info on Base Addresses</title><link>http://blogs.msdn.com/grantri/archive/2004/12/29/344014.aspx</link><pubDate>Thu, 30 Dec 2004 00:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:344014</guid><dc:creator>grantri</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/grantri/comments/344014.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=344014</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=344014</wfw:comment><description>&lt;p&gt;Another Microsoftie, &lt;A href="http://blogs.msdn.com/joshwil"&gt;Josh Williams &lt;/a&gt;follows my blogs and pointed out another case where a base address matters: NGEN.&amp;nbsp; When you NGEN your assemblies, the new images are loaded at the same base address as the original binaries.&amp;nbsp; As Josh pointed out to me, NGEN images do have significantly more relocations (similar to a real native image), and on 64-bit platforms &lt;em&gt;may&lt;/em&gt; end up using an extra jump stub when they get rebased far away from the call targets (i.e. when a 32-bit offset no longer works).&amp;nbsp; The relocations will affect load time, but the jump stubs will&amp;nbsp; affect runtime performance.&lt;/p&gt; &lt;p&gt;--Grant&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=344014" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item><item><title>More Q&amp;A - Why No Generic Attributes?</title><link>http://blogs.msdn.com/grantri/archive/2004/12/22/330208.aspx</link><pubDate>Wed, 22 Dec 2004 22:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:330208</guid><dc:creator>grantri</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/grantri/comments/330208.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=330208</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=330208</wfw:comment><description>&lt;p&gt;Question from Wes Haggard:&lt;/p&gt; &lt;p&gt;A while back I posted &lt;a title="http" href="http://weblogs.asp.net/whaggard/archive/2004/10/12/241476.aspx" target="_new"&gt;http://weblogs.asp.net/whaggard/archive/2004/10/12/241476.aspx&lt;/a&gt; about having a generic type inherit from Attribute. Do you know why this is prohibited in C#? &lt;br /&gt;&lt;/p&gt; &lt;p&gt;My attempted response:&lt;/p&gt; &lt;p&gt;I used to think it wasa a CLI restriction, but now I've scoured the docs and can't find anything like that.&amp;nbsp; So, you'll have to settle for a big fat "I don't know".&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=330208" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item><item><title>More Q&amp;A - C# Project settings</title><link>http://blogs.msdn.com/grantri/archive/2004/12/22/330160.aspx</link><pubDate>Wed, 22 Dec 2004 21:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:330160</guid><dc:creator>grantri</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/grantri/comments/330160.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=330160</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=330160</wfw:comment><description>&lt;p&gt;Question from Eric Wilson:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;Could you do a post on what the settings on the Advanced Tab in Visual Studio.Net for C# projects are for and when you should use them?&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;My lame response:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;I've only occasionally had to deal with the project-system guys that more or less own that 'Tab", so I can't even remember what's on it.&amp;nbsp; Going off of the online MSDN: &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vclrfadvancedconfigurationpropertiesprojectnamepropertypagesdialogbox.asp"&gt;Visual&amp;nbsp;C#&amp;nbsp;Reference Advanced, Configuration Properties, &amp;lt;Projectname&amp;gt; Property Pages Dialog Box&lt;/a&gt;, I see 4 settings: &lt;ul&gt; &lt;li&gt;Incremental Build - don't use it unless you absolutely need to.&amp;nbsp; It's getting removed in future versions.&amp;nbsp; It was a buggy attempt at trying to only recompile changed code and thus speed up compilation. &lt;li&gt;Base Address - For Class Libraries (DLLs) you can get a small speedup in load times if the OS doesn't have to relocate your DLL from it's preferred base address.&amp;nbsp; This is a big thing for native DLLs which typically have hundreds of relocations.&amp;nbsp; For managed DLLs there's at most one: the import of mscoree.dll!_CorDllMain.&amp;nbsp; As such this isn't really a huge win, but possibly still measurable.&amp;nbsp; It will also impact working-set across multiple processes.&amp;nbsp; Of course most big software houses don't use this setting, instead they run a tool like rebase.exe as a post-build step that lays out all of the DLLs so they all get unique addresses.&amp;nbsp;[&lt;A href="http://blogs.msdn.com/grantri/archive/2004/12/29/344014.aspx"&gt;More info in a new post&lt;/a&gt;] &lt;li&gt;File Alignment - For certain binaries, this enables you to save a little extra file space.&amp;nbsp; The 2 most common values are 4096, and 512.&amp;nbsp; I think the default is 4096.&amp;nbsp; By setting it to 512 you can &lt;strong&gt;potentially&lt;/strong&gt; reduce the ammount of file padding, but this &lt;strong&gt;can&lt;/strong&gt; hurt performance in other scenarios because now each section is not aligned the same as a memory page.&amp;nbsp; As with all performance issues, the best advice is to try it out and measure.&amp;nbsp; I expect this would have a bigger impact on the Compact Frameworks than on desktop applications. &lt;li&gt;Do not Use Mscorlib - The C# compiler automatically includes a reference to the mscorlib.dll that is installed with the current runtime.&amp;nbsp; If you want to use a different mscorlib.dll, you need to us this setting to disable the automatic reference, otherwise the 2 will conflict.&amp;nbsp; The most common use would be the Compact Frameworks.&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;--Grant&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font size="2"&gt;[12/29/04 - added link to new post on base addresses]&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=330160" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item><item><title>Why can't I do XYZ in C#?</title><link>http://blogs.msdn.com/grantri/archive/2004/12/07/277954.aspx</link><pubDate>Wed, 08 Dec 2004 00:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:277954</guid><dc:creator>grantri</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/grantri/comments/277954.aspx</comments><wfw:commentRss>http://blogs.msdn.com/grantri/commentrss.aspx?PostID=277954</wfw:commentRss><wfw:comment>http://blogs.msdn.com/grantri/rsscomments.aspx?PostID=277954</wfw:comment><description>&lt;p&gt;First off I'm not a language lawyer, or an expert.&amp;nbsp; I am only sharing some of the impressions I've gotten from working with the real language designers.&lt;/p&gt; &lt;p&gt;Eric Wilson asked &lt;A href="http://blogs.msdn.com/grantri/archive/2004/12/01/273265.aspx#273345"&gt;why C# doesn't allow you to call static methods using instance pointers&lt;/a&gt;.&amp;nbsp; My answer would be two-fold:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;C# is very explicit.&amp;nbsp; There's generally one and only one way to do a lot of things that in C/C++ had many ways.&amp;nbsp; Likewise whenever there was a C/C++ language construct that was often mis-used, misunderstood, or just confusing, the language guys tried very hard to make C# impossible to mis-use, misunderstand, or get confused.&amp;nbsp; This is such an example, see reason #2. &lt;li&gt;This is confusing to someone who reads the code.&amp;nbsp; If they don't have the definition of the method nearby, they won't know it's static and thus doesn't need a valid instance pointer (which Eric's sample code lacks).&amp;nbsp; Likewise some readers might try and assume that some sort of polymorphic virtual call might happen if the instance pointer is actually some sub class.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Orangy asked &lt;A href="http://blogs.msdn.com/grantri/archive/2004/12/01/273265.aspx#273755"&gt;why he can't derive from Delegates&lt;/a&gt;.&amp;nbsp; The short answer here is that it's a runtime restriction.&amp;nbsp; The runtime deals very intimately with delegates, and as such has some heavy requirements on them.&amp;nbsp; However, you can accomplish the same semantics with a little extra coding.&amp;nbsp; Basically create an intermediate class that holds a weak reference (this is not new in v2, just new to me, thanks Dmitriy) to the real delegate, then pass the intermediate delegate to the real event:&lt;/p&gt; &lt;p&gt;&lt;code&gt;// warning untested pseudo code&lt;br /&gt;sealed class WeakDelegate {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public EventHandler MakeWeakDelegate(EventHandler realDelegate) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new EventHandler(new WeakDelegate(realDelegate).WeakInvoke);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private volatile&amp;nbsp;WeakReference realDelegate; // volatile so it is thread safe&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private WeakDelegate(EventHandler realDelegate) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.realDelegate = new WeakReference(realDelegate);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void WeakInvoke(object sender, EventArgs args) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;EventHandler eh = this.realDelegate.Target as EventHandler;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (eh != null) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;eh.Target(sender, args);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt; &lt;p&gt;&lt;/code&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;That's all for today.&lt;/p&gt; &lt;p&gt;--Grant&lt;/p&gt; &lt;p&gt;&lt;font size="2"&gt;[Corrected the code and some comments in response to Dmitriy's criticism]&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=277954" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/grantri/archive/tags/C_2300_+Language/default.aspx">C# Language</category></item></channel></rss>