Introducing .NET 4.0 With VS 2010 读书笔记.1

New Types

Memory Mapping Files

    A memory mapped file maps the contents of a file into memory, allowing you to work with it in a very efficient manner. Memory mapped files can also be used for interprocess communication, allowing you to share information between two applications:

1. Add the following using statements:

using System.IO;
using System.IO.MemoryMappedFiles;

2. Main() method:

//Create a memory mapped file
using (MemoryMappedFile MemoryMappedFile = MemoryMappedFile.CreateNew("test", 100))
{
MemoryMappedViewStream stream
= MemoryMappedFile.CreateViewStream();
using (BinaryWriter writer =new BinaryWriter(stream))
{
writer.Write(
"hello memory mapped file!");
}
Console.WriteLine(
"Press any key to close mapped file");
Console.ReadKey();
}

3. Another Main() method:

//Read a memory mapped file
using (MemoryMappedFile MemoryMappedFile = MemoryMappedFile.OpenExisting("test"))
{
using (MemoryMappedViewStream Stream = MemoryMappedFile.CreateViewStream())
{
BinaryReader reader
=new BinaryReader(Stream);
Console.WriteLine(reader.ReadString());
}
Console.ReadKey();
}

The other main use of memory mapped files is for working with very large files.

 

SortedSet<T>
    Sorted set is a new type of collection in the System.Collections.Generic namespace that maintains the order of items as they are added. If a duplicate item is added to a sorted set, it will be ignored, and a value of false is returned from the SortedSet’s Add() method.
    The following example demonstrates creating a sorted list of integers with a couple of duplicates in:

SortedSet<int> MySortedSet =new SortedSet<int> { 8, 2, 1, 5, 10, 5, 10, 8 };

 

ISet<T> Interface
    .NET 4.0 introduces ISet<T>, a new interface utilized by SortedSet and HashSet and surprisingly enough for implementing set classes.


Tuple
    A tuple is a typed collection of fixed size. Tuples were introduced for interoperability with F# and IronPython, but can also make your code more concise. Tuples are very easy to create:

Tuple<int, int, int, int, int> MultiplesOfTwo = Tuple.Create(2, 4, 6, 8, 10);

    Individual items in the tuple can then be queried with the Item property:

Console.WriteLine(MultiplesOfTwo.Item2);

    Tuples might contain up to seven elements; if you want to add more items, you have to pass in another tuple to the Rest parameter:

var multiples =new Tuple<int, int, int, int, int, int, int,Tuple<int,int,int>>(2, 4, 6, 8, 10, 12, 14, new Tuple<int,int,int>(3,6,9));

    Items in the second tuple can be accessed by querying the Rest property:

Console.WriteLine(multiples.Rest.Item1);

 

System.Numerics.Complex
    Mathematicians will be glad of the addition of the new Complex type: a structure for representing and manipulating complex numbers, meaning that they will no longer have to utilize open source libraries or projects. Complex represents both a real and imaginary number, and contains support for both rectangular and polar coordinates:

Complex c1 =new Complex(8, 2);
Complex c2
=new Complex(8, 2);
Complex c3
= c1 + c2;

   

posted @ 2011-09-19 14:40  Stopwatch  阅读(233)  评论(0)    收藏  举报