Thursday, June 23, 2011

Satellite assembly

  • Satellite assemblies can only contain resources(.resources files/.resx files)
  • They cannot contain any executable code.
  • Satellites are loaded by a .NET class called System.Resources.ResourceManager.

  • It helps you to localize your assembly
  • For example, use different strings for different locales

  • One of the application of satellite assembly is multi language support
  • ie, keep separate resource file for each languages and load data from the resource files

  • Satellite assemblies can be installed in the GAC
http://www.codeproject.com/KB/aspnet/SatelliteAssemblies.aspx

Friday, May 27, 2011

3.5 performance related enhancements

Garbage Collection:

 
Change1:
  • New overloaded method added in System.GC class
  • public static void Collect(int generation, GCCollectionMode mode);
  • Method can be used to adjust the behavior for a forced garbage collection.
  • There are 3 GCCollectionMode available: Default, Forced, Optimized
  • Forced is the default mode
  • Forced – it forces garbage collection to occur immediately.
  • Optimized – it allow garbage collection to determine whether current time is optimal to reclaim objects

using System;
class Program
{
static void Main(string[] args)
{
 GC.Collect(2, GCCollectionMode.Optimized);
}

 
//GCCollectionMode.Forced is the default mode
GCCollectionMode.Default 

 
//allows garbage collection to determine whether current time is optimal to reclaim the objects
GCCollectionMode.Optimized 

 
//forces garbage collection to occur immediately
GCCollectionMode.Forced

 

 
Change2:
  • System.Runtime.GCSettings
  • The GCSettings class has a new LatencyMode property
  • It allows to adjust time that garbage collection intrudes in application.
  • GCLatencyMode enumeration has 3 values: (Batch,Interactive,LowLatency)
 
//disable garbage collection concurrency and reclaims objects in a batch call
//this is most intrusive mode
GCSettings.LatencyMode = GCLatencyMode.Batch;

 
//this is default mode of gc
//enable garbage collection concurrency and reclaims objects while application is running
//less intrusive(disturbing)
GCSettings.LatencyMode = GCLatencyMode.Interactive;

 
//enable garbage collection that is more conservative in reclaims objects
//full collection occurs only if the system is under memory pressure
//least intrusive
GCSettings.LatencyMode = GCLatencyMode.LowLatency;

 

 

 High performance hashset

 
  • HashSet is the new generic collection added in 3.5 framework
  • System.Collections.Generic
  • HashSet provides high performance set operations to the .net framework.
  • You can do UnionWith/IntersectWith on the HashSet objects
  •  Set collection does not contain duplicate elements.
  •  Elements of the set are in no particular order
  •  HashSet object’s capacity automatically increases as elements are added to the object
 
Parallel Programming
  •  
  • ·         Parallel Programming is breaking a large problem into smaller ones
  • ·         Carrying the execution of each chunk simultaneously. 
  • ·         There can be different forms of parallel computing:
  • o    bit-level
  • o     instruction-level
  • o    Data
  • o    task-parallelism.
 
Of these two forms, the ones that are important in the context of Task Parallel Library are Data- Parallelism and Task-Parallelism.

 
Data Parallelism
In data parallelism, the focus is on distributing data across different parallel computing nodes. For a multiprocessor system  executing a single set of instructions, data parallelism can be achieved by making each processor perform the same task on  different pieces of distributed data. There can be a single thread controlling this, or can be multiple threads. However, the  processing of data would be the same for each thread.

 
Task Parallelism
Task parallelism is the execution of a different thread by each processor on the same or different data. The threads may  execute the same or different code. For a simple example for a two processor machine, if we want to do two tasks, it will be  CPU ‘a’ doing task ‘A’ and CPU ‘b’ doing task ‘B’.

 
Task parallelism emphasizes the distributed (parallelized) nature of the processing (i.e., threads), as opposed to the data  (data parallelism).

3.5 ASP .Net Enhancements

-ASP .Net AJAX
-Silverlight
-New controls

ListView:
It has included styling with CSS, flexible pagination, and sorting, inserting, deleting, and updating features.
The ListView is a template driven control which means that it will not render anything. By default, the developer must completely specify the HTML he/she wants to render in the form of templates. To show data in ListView control, you need to take a LayoutTemplate (to define top level of HTML for output rendering). To show data, you need to take ItemTemplate and AlternativeItemTemplate to show alternative row as with different CSS. Here is the example of simple data binding with AlternativeItemTemplate.

DataPager:
The paging functionality for ListView is provided by a new control called DataPager.
The DataPager class is used to page data and to display navigation controls for data-bound controls that implement the IPageableItemContainer interface.

LinqDatasource

3.5 language features

Auto Implemented Properties:
Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.

-Object initializer
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor

private class Cat
{
    // Auto-implemented properties.
    public int Age { get; set; }
    public string Name { get; set; }
}

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

-Collection initialize
Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable.
List digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List digits2 = new List { 0 + 1, 12 % 3, MakeInt() };


List cats = new List
{
    new Cat(){ Name = "Sylvester", Age=8 },
    new Cat(){ Name = "Whiskers", Age=2 },
    new Cat(){ Name = "Sasha", Age=14 }
};


Extension Methods:
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.Extension methods are a special kind of static method.

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive

Implicit  local variable type inference:
Developers are allowed to declare the variables without mentioning the types by using the key word var.The compiler infers the type through its initialized values.
var i=10;
 i += 1;
 i = "Hello";

-Lamda expression:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates  or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies  the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is  read "x goes to x times x." This expression can be assigned to a delegate type as follows:
delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

Query expressions:
One common query language that is used to query any different data source.This common query language is none other than LINQ (Linear Integrated Query) Query Expressions.

Every Query expression of LINQ will have 3 different parts. They are:

•Creating and specifying data source.
•Defining required query expression.
•Execution of the Query.


-Partial Methods:
A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An  optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the  method and all calls to the method are removed at compile time.


-Nullable Type
A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is  assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;

-Covariance and Contravariance
Covariance and contravariance provide a degree of flexibility when matching method signatures with delegate types. Covariance  permits a method to have a more derived return type than what is defined in the delegate. Contravariance permits a method  with parameter types that are less derived than in the delegate type.

With contravariance, you can now use one event handler in places where, previously, you would have had to use separate  handlers