Some changes in .NET BCL 4.0

I’ve been porting a few products to .NET 4.0 and came across some cool new additions in .NET 4.0 which will be quite useful for developers.

Strings

Streams

Remember writing this before to copy one stream to another?

public static void CopyTo(this Stream input, Stream output)
{
  byte[] buffer = new byte[2048];
  while (true)
  {
    int read = input.Read (buffer, 0, buffer.Length);
    if (read <= 0)
    return;
    output.Write (buffer, 0, read);
  }
}

Now you don’t need to, just use the Stream.CopyTo() method.

inputStream.CopyTo(output);

Checking for 64bit-ness

Previously to detect a 64bit operating system you would either P/Invoke out and call the IsWow64Process in Kernel32, looked at the “PROCESSOR_ARCHITECTURE” environment variable or even easier (and completely managed code) way of checking the size of a Pointer.

public static bool IsWin64
{
  return (IntPtr.Size == 8);
}
public static bool IsWin32
{
  return (IntPtr.Size == 4);
}

Now you can simply use the Environment class that comes with two new properties.

WPF 4.0 Improvements

There are simply too many to list, see the article on ScottGu‘s blog about WPF4 and VS2010/.NET 4.0.

One very important tweak are the Text Rendering improvements that TextBlock‘s now have a new TextOptions.TextFormattingMode that greatly improves the quality of text rendering.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
            <TextBox TextOptions.TextFormattingMode="Ideal" FontSize="11">ThushanFernando.com - Ideal</TextBox>
            <TextBox TextOptions.TextFormattingMode="Display" FontSize="11">ThushanFernando.com - Display</TextBox>
            <TextBox TextOptions.TextFormattingMode="Ideal" FontSize="16">ThushanFernando.com - Ideal</TextBox>
            <TextBox TextOptions.TextFormattingMode="Display" FontSize="16">ThushanFernando.com - Display</TextBox>
        </StackPanel>
    </Grid>
</Window>

Here’s a pretty picture showing the difference between using Ideal and Display. The difference is noticeable for text sizes below 15.

Difference between Ideal &amp; Display.
Figure 1. Difference between Ideal &amp; Display.

Alternatively you can place it in the Window so all child controls will render nicely.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow - Display" Height="350" Width="525" TextOptions.TextFormattingMode="Display">
    <Grid>
        <StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
            <TextBox FontSize="11">ThushanFernando.com</TextBox>
            <TextBox FontSize="16">ThushanFernando.com</TextBox>
        </StackPanel>
    </Grid>
</Window>

There are LOTS more coming in .NET 4.0 that will make anyone doing .NET development today just wet their pants over, just read the article on MSDN by Justin Van Patten about Whats new in the BCL in .NET 4.0 and also posted on the BCL team blog.

Related Articles

Comments have been disabled.