Saturday, December 15, 2012

.Net 4.5 Features, what is new?



  • Cross-Platform Development with the .NET Framework


  • Portable class libraries on multiple platforms, such as Windows 7, Windows 8, Silverlight, Windows Phone, and Xbox 360. When you create a Portable Class Library project, you can choose two or more platforms you want to target. Link: http://msdn.microsoft.com/en-us/library/gg597391.aspx


  • Asynchronous file operation

  • In the .NET Framework 4.5, new asynchronous features were added to the C# and Visual Basic languages. These features add a task-based model for performing asynchronous operations. To use this new model, use the asynchronous methods in the I/O classes. 


  • perform resource-intensive I/O operations without blocking the main thread.
  • async method contains Async in its name such asReadAsync, WriteAsync, CopyToAsync, FlushAsync, ReadLineAsync, and ReadToEndAsync. These async methods are implemented on stream classes, such as Stream,FileStream, and MemoryStream, and on classes that are used for reading from or writing to streams, such TextReader and TextWriter
  • In th .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous I/O operations. These methods are still available in the .NET Framework 4.5 to support legacy code; however, the async methods help you implement asynchronous I/O operations more easily.
  • Keywords – async (mark method which contains async operaion) and await (applies to result of async operation)

  • Ref URL : http://msdn.microsoft.com/en-us/library/kztecsys.aspx



  • Web
  • - Support for reading and writing HTTP requests and responses asynchronously.
    - Support for new HTML5 form types
    - Support for asynchronous modules and handlers.
    Ref URL : http://msdn.microsoft.com/en-us/library/hh420390.aspx



  • CLR and Framework improvements

  • a.    Ability to reduce system restarts by detecting and closing .NET Framework 4 applications during deployment
    b.    Support for arrays that are larger than 2 gigabytes (GB) on 64-bit platforms. This feature can be enabled in the application configuration file. Ref: element
    c.    Better performance through background garbage collection for servers.
    d.    ProfileOptimization - Background just-in-time (JIT) compilation, which is optionally available on multi-core processors to improve application performance. (Profile optimization requires a multicore computer. The methods are ignored on other computers.) Each time you initiate profile optimization in an application domain, the profile that was created during the previous use is read. The information in the profile is used to guide background compilation by identifying the methods that are most likely to be executed during startup. On multicore computers, this increases the chances that a method is already compiled by the time it is needed so that the main application thread does not have to call the JIT compiler.
    e.    Regex.MatchTimeout property - The MatchTimeout property defines the approximate maximum time interval for a Regex instance to execute a single matching operation before the operation times out.
    f.    Ability to define the default culture for an application domain
    g.    Zip compression improvements to reduce the size of a compressed file. See the System.IO.Compression namespace.
    h.    Type reflection support split between Type and TypeInfo classes




  • Improved Garbage Collection


  • Background GC available to server application also in .Net 4.5
    Before version 4, the .NET Framework provided a concurrent GC mode that performed full GCs concurrently with user code (vs. blocking, which pauses all user threads), thus reducing pause time for full GCs.

    In the .NET Framework 4, Microsoft delivered an improved version called the background workstation garbage collection, which reduced latency but only benefited client apps.

    In the .NET Framework 4.5, Microsoft have delivered background server garbage collection, which is typically used for server apps. As a result, all apps now have background GC available to them, regardless of which GC they use.
    This results in much shorter pauses on server.

    Higher throughput
    For server GC, there’s one heap per logical processor.
    When one of the heaps runs out of space for allocations, it triggers a GC. Usually, server apps have a pool of worker threads that perform similar types of tasks, and roughly the same amount of memory is allocated per thread, which results in naturally balanced managed heaps. If this is not the case, and one thread allocates a lot more memory than other threads, the heap that corresponds to its CPU runs out of space to allocate quickly and triggers a GC. GC has a “heap balancing” mechanism to help these apps.

    In the .NET Framework 4 and earlier versions, the small object heap (SOH) was balanced, but the large object heap (LOH) was not, so imbalanced LOH allocations in server GC would trigger more full GCs . In the .NET Framework 4.5, the server GC allocator balances the allocations across the heaps when it finds that GC heaps, including both SOH and LOH, are not balanced. In apps that begin to develop imbalanced heaps, this mechanism eliminates unnecessary GCs. This reduces the total time spent in GC and improves the app throughput.

    GC support for latest hardware
    Starting with Windows 7 and Windows Server 2008 R2, Windows supports more than 64 processors on a single computer. For machines that have very large numbers of processors (more than 64), the operating system splits the processors into multiple processor groups. In the .NET Framework 4.5, the GC can view CPUs across processor groups and takes all the cores into account when creating and balancing heaps.

    You’ll need to enable the element in your app’s configuration file to turn this behavior on. GC automatically detects and enables NUMA support. (NUMA – Non uniform memory access)

    SustainedLowLatency
    Stock market kind of application cannot tolerate pauses.

    In the .NET Framework 4.5, Microsoft has provided the option by introducing SustainedLowLatency mode, which avoids full blocking GCs. This mode is also available for the workstation GC in the .NET Framework 4 via Update 4.0.3. While the SustainedLowLatency setting is in effect, generation 0, generation 1, and background generation 2 collections still occur and do not typically cause noticeable pause times. blocking generation 2 collection happens only if the machine is low in memory or if the app induces a GC by calling GC.Collect().

    In the .NET Framework 4.5, SustainedLowLatency mode is available for both workstation and server GC. To turn it on, set the GCSettings.LatencyMode property to GCLatencyMode.SustainedLowLatency. The .NET Framework 4 includes aLowLatency mode for workstation GC; however, this setting is only intended to be used for short periods of time, whereasSustainedLowLatency mode is intended to be used for much longer.

    Applications work with large datasets where user objects > 2 GB
    In earlier versions of the .NET Framework, the following lines of code throw OutOMemoryException exceptions due to the 2-GB limit on object sizes on the GC heap. In the .NET Framework 4.5, the code succeeds, assuming that there is enough physical memory available on the machine:

    new Object[1000000000]; //(approximate size of this array 8 GB)
    new int[50000,50000]; //(approximate size of this array is 10 GB)
    new Dictionary(1000000000); //(approximate underlying array size is 24 GB)


    Note that the array index is still a 32-bit number. By default, the .NET Framework does not support arrays that are greater than 2 GB, but in the .NET Framework 4.5, you can use the element in your application configuration file to enable arrays that are greater than this size.

    Sunday, June 10, 2012

    Playing with Constructors (c# 4.0) (Examples in VS 2010)

    Constructors are class methods which executes when class object is created. 
    Constructors can be marked as public, private, protected, internal, protected internal, static.
    Classes can have multiple constructors. If we do not specify constructor, c# will create default one.

    Scenario 1: Inheritance and default constructors

    When we inherit base class (Base1) in child class (Base2), and create an object of child class, it executes base class constructor first and child class constructor later. This helps that when child class constructor is executing, system makes sure that base class has already set properties or initializations and then child class can re-set those according to need. Refer below example for this:

    class Base1
    {
        public Base1()
        {   Console.WriteLine("Base 1 .ctor called");
        }
    }
    class Base2 : Base1
    {
        public Base2()
        {   Console.WriteLine("Base 2 .ctor called");
        }
    }

    Actual call is
    Base2 base2Obj = new Base2(); or
    Base1 base1Obj = new Base2();

    Output is

    Base 1 .ctor called
    Base 2 .ctor called

    Even if you write base2 constructor as below, output will be the same as mentioned above.

    public Base2() : base()
    {       
         Console.WriteLine("Base 2 .ctor called");
    }

    Conclusion:

    It always the base class costructor that  get called first. Derived class may not like the way that the base class has been initialized. It can change the initial value of data provided that it has access to do so.