views:

1351

answers:

17

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?

+50  A: 

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
Awesome! exactly what I meant!! I will surely use that!
Faruz
+7  A: 

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:

herzmeister der welten
+10  A: 

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

Fahad
Well, didn't know that either. Could save lots of useless coding...
Faruz
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
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
@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
@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
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
+1  A: 

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
A: 

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
+3  A: 

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
+12  A: 

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

Stustu
Plus it now shows an example of XML transformations for Debug/Release.
Developer Art
Yes! I really like to keep things as simple as possible
Junior Mayhé
+11  A: 

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.

Hightechrider
+3  A: 

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.

Hightechrider
good thing. some is better than none.
Anonymous Type
+10  A: 

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
about damn time..
corymathews
+2  A: 

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.

Dennis Delimarsky
+10  A: 

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
+4  A: 

Enum.TryParse

Guid.TryParse

Allrameest
+1  A: 

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
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
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
+2  A: 

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
+2  A: 

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

System.Numerics.Complex - Represents a complex number.

brickner
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
A: 

Specific to ASP.NET MVC.

Instead of <%= Html.Encode(...) %>

You can do <%: ... %>

The colon syntax is short hand for Response.Write and HTML encoding in one. It sure does make your views nicer on the eyes.

Finglas