{"id":1039,"date":"2010-07-04T17:27:02","date_gmt":"2010-07-04T11:27:02","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=1039"},"modified":"2025-04-07T20:31:32","modified_gmt":"2025-04-07T20:31:32","slug":"get-ready-for-c-4-0","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/get-ready-for-c-4-0\/","title":{"rendered":"New Features in C# 4.0"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" src=\"http:\/\/www.omersarikaya.com\/ckfinder\/userfiles\/images\/logo1[1].jpg\" alt=\"\" width=\"316\" height=\"209\" \/>Visual Studio 2010 is packed with new and enhanced features that simplify the entire development process from design to deployment. Customize your workspace with multiple monitor support. Create rich applications for SharePoint and the Web. Target multiple versions of the .NET Framework with the same tool. Eliminate the dreaded &#8220;no repro&#8221; problem with IntelliTrace. And much more.<\/p>\n<p><a href=\"http:\/\/www.microsoft.com\/visualstudio\/en-us\/download\">Visual Studio 2010 is here!<\/a>And of course this means that C# 4.0 is also here. Let\u2019s do a quick review of the new language features added in this release.<\/p>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<h4>Dynamic<\/h4>\n<p>The\u00a0<strong>dynamic<\/strong> keyword is a key feature of this release. It closes the gap between dynamic and statically-typed languages. Now you can create dynamic objects and let their types be determined at run time. With the addition of the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.dynamic(v=VS.100).aspx\">System.Dynamic<\/a>namespace, you can create expandable objects and advanced class wrappers, and you can provide interoperability between different languages, including dynamic ones. Here is one quick example:<\/p>\n<blockquote><p>dynamic\u00a0contact =\u00a0new\u00a0ExpandoObject();<\/p>\n<p>contact.Name =\u00a0&#8220;Patrick Hines&#8221;;<\/p>\n<p>contact.Phone =\u00a0&#8220;206-555-0144&#8221;;<\/p><\/blockquote>\n<h4>Covariance and Contravariance<\/h4>\n<p>Variance on generic type parameters in interfaces and delegates is another important feature of this release. It doesn\u2019t add much new functionality, but rather makes things work as you expected them to in the first place. The major advantage is hidden in this simple line, which didn\u2019t compile until C# 4.0:<\/p>\n<div>\n<blockquote>\n<div>IEnumerable&lt;Object&gt; objects =\u00a0new\u00a0List&lt;String&gt;();<\/div>\n<\/blockquote>\n<\/div>\n<div>The ability to implicitly convert references for objects instantiated with different type arguments makes it much easier to reuse code. Read the\u00a0<a href=\"http:\/\/blogs.msdn.com\/csharpfaq\/archive\/2010\/02\/16\/covariance-and-contravariance-faq.aspx\">Covariance and Contravariance FAQ<\/a> to learn more about this feature.<\/div>\n<div>\n<h4>Optional (or Default) Parameters<\/h4>\n<p>People have been asking for this feature since C# 1.0. Three versions later, it\u2019s finally here.<\/p>\n<p>Now you can assign a default value to a parameter right within the method declaration. The user of the method can either pass a value or simply skip the argument. In the latter case, the default value is passed to the method.<\/p>\n<p>Method declaration:<\/p>\n<blockquote><p>public\u00a0static\u00a0void\u00a0SomeMethod(int\u00a0optional = 0) { }<\/p><\/blockquote>\n<div>\n<div>Method calls:<\/div>\n<blockquote>\n<div>SomeMethod();\u00a0\/\/ 0 is used in the method.<\/p>\n<p>SomeMethod(10);<\/p>\n<\/div>\n<\/blockquote>\n<div>\n<h4>Named Arguments<\/h4>\n<p>The order of parameters in a method declaration and the order of arguments you pass when calling the method don\u2019t need to match anymore. You can provide arguments in any order you like by specifying parameter names in a method call. This might also improve the readability of your code.<\/p>\n<blockquote><p>var\u00a0sample =\u00a0new\u00a0List&lt;String&gt;();<\/p>\n<p>sample.InsertRange(collection:\u00a0new\u00a0List&lt;String&gt;(), index: 0);<\/p>\n<p>sample.InsertRange(index: 0, collection:\u00a0new\u00a0List&lt;String&gt;());<\/p><\/blockquote>\n<p>Read more about optional parameters and named arguments on\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd264739(VS.100).aspx\">MSDN<\/a>.<\/p>\n<\/div>\n<div><strong>Improved COM Interop<\/strong><\/div>\n<\/div>\n<p>The introduction of the\u00a0<strong>dynamic<\/strong> keyword, optional parameters and named arguments enables improvement of COM interop. So, no more ugly code like this:<\/p>\n<blockquote><p>var\u00a0excelApp =\u00a0new\u00a0Excel.Application();<\/p>\n<p>\/\/ . . .<\/p>\n<p>excelApp.get_Range(&#8220;A1&#8221;,\u00a0&#8220;B4&#8221;).AutoFormat(<\/p>\n<p>Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3,<\/p>\n<p>Type.Missing,\u00a0Type.Missing,\u00a0Type.Missing,<\/p>\n<p>Type.Missing,\u00a0Type.Missing,\u00a0Type.Missing);<\/p><\/blockquote>\n<p>You can now simply write the following:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<blockquote><p>excelApp.Range[&#8220;A1&#8221;,\u00a0&#8220;B3&#8221;].AutoFormat(<\/p>\n<p>Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);<\/p><\/blockquote>\n<p>By the way, this code uses one more new feature:\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee310208(VS.100).aspx\">indexed properties<\/a> (take a closer look at those square brackets after\u00a0<strong>Range<\/strong>.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0. For more information about new COM interop features, once again, refer to\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd264733(VS.100).aspx\">MSDN<\/a>.<\/p>\n<h4>What Else?<\/h4>\n<p>Of course, C# benefits not only from new language features, but also from improvements to its integrated development environment (IDE) and to the .NET Framework.<\/p>\n<p>Here are some links for further reading:<\/p>\n<ul>\n<li>Find all calls to and from your methods with the new\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd409859(VS.100).aspx\">Call Hierarchy<\/a> window.<\/li>\n<li>Even if you have never heard of\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Test-driven_development\">test-driven development (TDD)<\/a>, the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd409796(VS.100).aspx\">Generate From Usage<\/a> feature may help you to quickly prototype your application by generating method and class stubs, allowing you to concentrate on your current task rather than worrying about declarations. For TDD adepts, this feature might be a real performance booster. Check out\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd998313(VS.100).aspx\">this walkthrough<\/a> for more details.<\/li>\n<li>Don\u2019t forget to take a look at\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms171868(VS.100).aspx\">What&#8217;s New in the .NET Framework 4<\/a> and also at\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb383815(VS.100).aspx\">What&#8217;s New in Visual C# 2010<\/a> (just to make sure I didn\u2019t miss something).<\/li>\n<li>And just in case, check out\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee855831(VS.100).aspx\">breaking changes for C#<\/a> and\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee941656(VS.100).aspx\">.NET Framework<\/a>.<\/li>\n<li>Last, but not least, don\u2019t forget to report any problems at\u00a0<a href=\"http:\/\/connect.microsoft.com\/VisualStudio\">Microsoft Connect<\/a>.<\/li>\n<li><strong>Update.<\/strong> See a post:\u00a0<a href=\"http:\/\/blogs.msdn.com\/csharpfaq\/archive\/2010\/05\/10\/new-ide-features-in-visual-studio-2010-for-c-developers.aspx\">New IDE Features in Visual Studio 2010 for C# Developers<\/a>.<\/li>\n<\/ul>\n<p>Happy coding<br \/>\n<img decoding=\"async\" src=\"http:\/\/s07.flagcounter.com\/count\/dYL\/bg=FFFFFF\/txt=000000\/border=FFFFFF\/columns=6\/maxflags=200\/viewers=0\/labels=1\/pageviews=1\/\" border=\"0\" alt=\"free counters\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2010 is packed with new and enhanced features that simplify the entire development process from design to deployment. Customize your workspace with multiple monitor support. Create rich applications for SharePoint and the Web. Target multiple versions of the .NET Framework with the same tool. Eliminate the dreaded &#8220;no&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[28,29,166,247,295,358,534,791,828,1073],"class_list":["post-1039","post","type-post","status-publish","format-standard","hentry","category-random","tag-net","tag-net-framework","tag-asp-net","tag-c-4-0","tag-covariance-and-contravariance","tag-dynamic","tag-improved-com-interop","tag-named-arguments","tag-optional-or-default-parameters","tag-visual-studio-2010"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=1039"}],"version-history":[{"count":1,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1039\/revisions"}],"predecessor-version":[{"id":4447,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1039\/revisions\/4447"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=1039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=1039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=1039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}