71

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?

Faruz
  • 9,909
  • 10
  • 48
  • 66

16 Answers16

66

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.

Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107
21

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.

Rafa Castaneda
  • 812
  • 1
  • 7
  • 13
18

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.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
15

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

Faruz
  • 9,909
  • 10
  • 48
  • 66
13

I just love the fact that web.config file is small and meaningful, instead of long and full of unknown statements...

Stustu
  • 158
  • 1
  • 10
12

Optional parameters is one of my favorites. The dynamic type seems to be promising

Fahad
  • 2,903
  • 3
  • 18
  • 13
  • 1
    Well, didn't know that either. Could save lots of useless coding... – Faruz Apr 22 '10 at 12:11
  • Relevant SO Question you could maybe add to this answer: http://stackoverflow.com/questions/2690623/what-is-the-dynamic-type-in-c-4-0-used-for – ParmesanCodice Apr 22 '10 at 12:43
  • 3
    Just be careful with Optional Parameters on public/virtual methods: http://www.stum.de/2010/04/19/how-optional-parameters-work-why-they-can-be-dangerous-and-why-they-work-in-net-23-as-well/ - They are safe on internal/private classes though. – Michael Stum Apr 23 '10 at 22:55
  • @Michael - thumbs up to your comment and blog entry. I don't need to save myself the typing so badly that I'll risk compiling a parameter value into my call site . – Jeff Sternal Apr 27 '10 at 19:05
  • @Jeff You're welcome. As said, you're perfectly fine for internal/private methods as all callsites are recompiled anyway. Just on public it's not a good idea :) – Michael Stum Apr 27 '10 at 19:22
  • True, but it's another thing that gets in the way of refactoring. For such a little payoff, I just can't see it ever being worth it (except perhaps in cases where you have obscene numbers of parameters, in which case there may be a *different* problem at work). – Jeff Sternal Apr 27 '10 at 19:51
9

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:

Sebastian Ullrich
  • 1,170
  • 6
  • 10
herzmeister
  • 11,101
  • 2
  • 41
  • 51
9

Enum.TryParse

Guid.TryParse

Allrameest
  • 4,364
  • 2
  • 34
  • 50
5

System.Numerics.BigInteger - Represents an arbitrarily large signed integer.

System.Numerics.Complex - Represents a complex number.

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
brickner
  • 6,595
  • 3
  • 41
  • 54
  • 1
    very cool [from MSDN] => The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. Because the BigInteger type is immutable and because it has no upper or lower bounds, an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large. – Anonymous Type Sep 02 '10 at 00:47
4

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.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
4

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

Henry99
  • 269
  • 3
  • 12
3

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.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
3

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

sqlray
  • 146
  • 4
  • Actually, the ASP.NET cache is still in the System.Web assembly just as it was before. I'm not really sure why. (BTW, the new cache seems promising.) – Venemo May 22 '10 at 21:00
  • I am sure it is for backwards compatibility. From what I have heard the system.web.caching.cache will not be enhanced in future releases. – sqlray May 25 '10 at 12:34
2

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.

Den
  • 16,686
  • 4
  • 47
  • 87
1

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)

Faruz
  • 9,909
  • 10
  • 48
  • 66
0

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.

vfilby
  • 9,938
  • 9
  • 49
  • 62