Things in .NET Framework 4 that every programmer should know [closed]

I recently moved to Visual Studio 2010 and upgraded my website 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?


Solution 1:

  • 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.

Solution 2:

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.