Home > .NET > Tidbits in dotNET Framework 4

Tidbits in dotNET Framework 4

Kevin:Tidbits of .NET

June 30th | 2010

.NET Tidbits

Every now and then while exploring the .NET Framework 4 I come across something small, but very useful. In no particular order, here is a list of the top 10 new small changes to the framework that may help.

1. Environment.Is64BitOperatingSystem

This is a huge timesaver. Doing this previously required Platform Invoke to do properly (IntPtr.Size doesn’t work if your assembly is compiled for x86 only). Likewise, it has an additional helper Environment.Is64BitProcess. They aren’t something that will get used on a daily basis, but it’s great to have them when you need them.

2. System.Numerics

This is a namespace in a new assembly called System.Numerics. It contains two very useful types: BigInteger and Complex. BigInteger, like it sounds, is big. Unlike the primitives that already existed, such as Int32, Int64, etc; this has absolutely no limit to how big the number can be.Possible applications are calculators and cryptography when dealing with large prime numbers. Complex is a complex number that is used to represent number with an imaginary component, such as the square root of -1. The ToString implementation of this is disappointing. I would expect it to be “a + bi” notation, but rather uses “(a, b)”.

3. System.Lazy<>

This is class to better support lazy initialization of objects. A typical pattern would be a service locator that may or may not get all of its services used. Rather than the service locator setting everything up on startup, you can use the Lazy class internally to create the service when it is needed. It even has a constructor overload for thread safety. Here is a snippet:

public class ServiceLocator
{
    private readonly Lazy<IMyService> _lazyMyService
        = new Lazy<IMyService>(() => new MyService(), true);
    public IMyService MyService
    {
        get
        {
            return _lazyMyService.Value;
        }
    }
}

4. IEnumerable.Zip extension

A new extension method that is a part of LINQ is Zip. This allows you to merge two sequences together based on a predicate. It’s quite simple and was something that many considered an oversight in the original release of LINQ. Oversight or not, here it is!

5. Enum.TryParse

Ever been given an enumeration value in the form of a string, and needed to parse it in a safe way? Well now you can with the introduction of Enum.TryParse. It will not throw an exception like Parse will, and follows the same semantics as Int32.TryParse by returning a Boolean to indicate if it was successful or not, and using an “out” parameter to actually return the value.

6. System.Tuple

Many developers have been asking for Tuple support. A Tuple is an immutable structure that can contain two or more elements. Simple as that. They are created using Tuple.Create(), and support up to eight items. More dynamic languages, including the new F# language, make heavier use to tuples and are created using their own syntax.

7. System.Collections.StructualComparisons

Ever want to compare two arrays to see if they were the same? Sounds simple up front, but usually requires some sort of loop or LINQ operation. Now it’s possible to compare two arrays to see if their contents are the same, not if they are the exact same array. Here is snippet to illustrate the use:

var strings1 = new[] {"Hello", "World!"};
var strings2 = new[] {"Hello", "World!"};
int compared = StructuralComparisons.StructuralComparer.Compare(strings1, strings2);

In this case, the comparer returns 0 because there is no difference between the two arrays.

8. System.IO.File.ReadLines

ReadLines is a helpful method that allows you to lazily enumerate over the lines of a text file, rather than using ReadAllLines which isn’t efficient for large text files.

foreach(var line in File.ReadLines(".\\myfile.txt"))
{
    //parse the line and move on to the next one
}

9. MemoryMappedFile

This class is part of the System.IO.MemoryMappedFiles namespace. Its purpose is to provide a managed implementation of a memory mapped file. This is often desirable when working with extremely large files and remaining efficient. To do the same in previous versions of the .NET Framework, you would have to relied on platform invoke.

10. System.Net.Mail.SmtpClient.Dispose

This is more of a “gotcha” than a feature, but it also cleans up a known issue in the .NET Framework. The SmtpClient now implements IDisposable. This gives you control over when the SmtpClient sends a QUIT message to the SMTP server. If this isn’t called after you complete sending emails, it will leave the connection open and may result in unexpected behavior.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

  1. Rahul
    January 24, 2011 at 9:10 am

    Hi Dev, All the following top 10 new small changes to the framework were very useful and informative. Thanks for youe effort and sharing your knowledge.
    Environment.Is64BitOperatingSystem
    System.Numerics
    System.Lazy
    IEnumerable.Zip extension
    Enum.TryParse
    System.Tuple
    System.Collections.StructualComparisons
    System.IO.File.ReadLines
    MemoryMappedFile
    System.Net.Mail.SmtpClient.Dispose
    I like your way of approach in explaining the topic. Its very nice and easy to follow.

    http://www.gloriatech.com/microsoft-net-development-services.aspx

  2. vidhya s
    July 19, 2011 at 7:17 am

    it is really informative and useful..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: