Things in .NET Framework 4 that every programmer should know [closed]
I recently moved to Visual Studio 2010 and upgraded my websit开发者_开发百科e to work with .NET Framework 4. (From VS 2008 - Framework 3.5)
What are things I need to know to improve site speed, readability or memory use?
Parallel for loops
Parallel.For(0,10,(i)=> { // Do stuff in parallel. });
The rest of the Parallel class provides some other great things like Parallel.Invoke(...)
and Parallel.ForEach(...)
.
Also, if you do anything with Linq, you can use the ParallelEnumerable.AsParallel() Method to convert your Linq queries to run in parallel.
It's all built on the Task Parallel Library which provides a great set of API's for working with tasks in an abstracted way that scales for whatever resources your machine has without having to think too much about exactly how many threads you are creating.
The DirectoryInfo
class in addition to the GetDirectories
and GetFiles
methods now has their lazy versions EnumerateDirectories
and EnumerateFiles
, which avoid us to have large arrays to hold all the objects at once.
string.Join()
now has a signature that takes IEnumerable<T>
instead of just string[]
- a small improvement that allows you to rip out your .Select()
and .ToArray()
code.
Awesome thing, Client IDs can be manipulated:
http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx
No more CTL0001$_DIV0003_TextBox1001$ or whatever...
I just love the fact that web.config file is small and meaningful, instead of long and full of unknown statements...
Optional parameters is one of my favorites. The dynamic
type seems to be promising
The way how C# implements event
fields is new. It no longer internally does a very bad lock (this)
by default. Events are still thread-safe however because an Interlocked.CompareExchange(...)
mechanism is now used instead.
This lead to some changes that could be breaking in some edge cases. More info:
- Chris Burrows' Blog: Events get a little overhaul in C# 4, part I: Locks
- Chris Burrows' Blog: Events get a little overhaul in C# 4, part II: Semantic Changes and +=/-=
- Channel 9: Whirlwind 14: What's new in C# 4 - Events
Enum.TryParse
Guid.TryParse
System.Numerics.BigInteger - Represents an arbitrarily large signed integer.
System.Numerics.Complex - Represents a complex number.
Code contracts look very promising both from the standpoint of creating more correct code but also from the point of producing more complete documentation. Sadly they aren't all there in VS2010 yet - you have to install an add-on and even then it's neither complete nor finished and appears to still be a work in progress.
You can use memory-mapped files (in the same way that native Windows functions access memory-mapped files) to edit very large files and to create shared memory for interprocess communication. For a detailed explication see: http://msdn.microsoft.com/en-us/library/dd997372.aspx
For ASP.NET programmers the ASP.NET 4 and Visual Studio 2010 Web Development Overview white paper gives a comprehensive overview of what is new in ASP.NET 4. For a series of articles on the most prominent and interesting changes I'd recommend Scott Gutherie's series of blog posts on VS 2010 and .NET 4 Series.
The ASP.net cache is now in its own assembly!
System.runtime.caching.dll
which means you can use in other apps like WPF and WinForms without having to pull in the whole system.web assembly
I just wish they would have beefed up the CacheItem to include built-in information about the cache item like when it was added...when it will expire, etc
I would also refer to original documentation (MSDN in this case) for a comprehensive list of improvements and additions:
http://msdn.microsoft.com/en-us/library/ms171868.aspx
From that article you can easily find the things that can improve the existing code base.
For the sake of readability, I'll add my discovery as written in the question it self.
When using AJAX, you can specify EnableCdn property for the scriptManager to load values from CDNs (such as Microsoft CDN)
I believe there are also enhancements to WCF that eliminate previous annoyances like not being able to configure WebGet
/WebInvoke
differently for each endpoint in .Net 3.5. I believe it is fully configurable in 4.0.
精彩评论