Sunday, February 8, 2009

Generics

Generics - one of the mechanism of code reuse:algorithm reuse.
Generics allow you to define type-safe data structures, without committing to actual data types.
  • some one creates a an algorithm with generic data type and everyone can use it for their own data types.
  • CLR allow the creation of generic: Reference type, Value Type, Interfaces, Delegates, Methods but not of Enum type.
  • Advantages:
    • Compile time Type Safety
    • Cleaner Code as fewer casts are required.
    • Better perormance: before generics generic algo are made based on object data type so for any value type it require boxing which hurts performance. Now algo is defined for specific data type.
  • While performance improvement using generics are not much but it offers other benefits as like compile time type safety and cleaner code.
  • If actual datatype is passed in for all of the type arguments, the type is called closed type e.x.Dictionary. Instance of closed type can be created.
  • If some generic type argument left unspecified - it is open type object in CLR e.x. Dictionary. Instance of open type can not be created.
  • Code Explosion: Each time a generic method is called for a type it's native code is generated for that type so CLR will generate native for every method/type combination i.e. there will be two native code for List, List - code explosion. To avoid this CLR has some optimizations like: All reference type are considered identical, Two assemblies referring generic of same datat type will share same native code. But for value type code will explode as they have diff sizes or diff cpu instructions.
  • Generic type aliasing : wrting too many brackets in code <> make it un readable so u can use "using" for cleaner code - using listOfString = List; listOfString lss = new listOfString();
  • Arity: Number of Type parameters required by the Type. Dictionary arity -2, DictionaryStringKey - arity 1.
  • Generic Examples:
  • private static T Min ( T 01,T02) { if(o1.CompareTo(o2)<0>
  • Primary Constraints or Reference/Value type constraints.
    • Reference type specify that argument type is a class that is not sealed and arg type is of specified type or it's dervied type.
    • Value type specify that type arg will be a value type.
    • Ex: interanl sealed class PrConstr where T:class{} (in this T can be set to null inside the class as it is sure to be of reference type.)
    • Ex: interanl sealed class PrConstr where T:struct{} - it actually means that T will have a paramerter less default constr ( as all value type has it).
    • Type param can specify zero or one primary constraints.

  • Secondary Constraints or Derivation constraints.
    • A derivation constraint indicates to the compiler that the generic type parameter derives from a base type such an interface or a particular base class.
    • Can have zero or more sec constraints.
    • Ex in previous example of min:
    • T Min ( T 01,T02) where T:Icomparable { if(o1.CompareTo(o2)<0> - now this will make sure that type parameter T will have compareTo method.

  • Constructor Constraints: Suppose you want to instantiate a new generic object inside a generic class. The problem is the C# compiler does not know whether the type argument the client will use has a matching constructor, and it will refuse to compile the instantiation line. is done using the new() constraint.
    • A default constructor constraint indicates to the compiler that the generic type parameter exposes a default public constructor (a public constructor with no parameters).
    • Ex: internal sealed Class contrConst where T: new() {}
    • can have zero or one constructor constraints.

  • CLR doesn't allow overloading based on type parameter name or constraints. Overloading is based only on arity.
  • In Overriding number of type parameter must be same and overriding mthd can't define constraints it will inherit from base method.
  • Default Value : default key word allow developers to set a generic type param to default.
  • Ex - T temp = default(T) - this will set T to null if T is ref type or zero if T is value type.
  • Comparing to null is allowed without any constraints.
  • Comparing two variables of same generic type is illegal unless type parameter is known to be reference type.
  • .NET allows you to have serializable generic types:
  • Under .NET 2.0, you cannot define generic Web services.That is, Web methods that use generic type parameters. The reason is that none of the Web service standards support generic services.
  • You also cannot use generic types on a serviced component. The reason is that generics do not meet COM visibility requirements, which are required for serviced components

.Net 1.1 vs 2.0

The following sections of this topic describe changes made in different areas of ASP.NET:

Web Site Management
Web Site Administration Tool(wizard to configure web site)
Reserved Folders for Special Functionality
App_code - any class in this folder compiles automatically.App_Date - for DB.
Precompiling Web Sites for Error Checking and Deployment
Navigation - site map for menu.Treeview and siteMappath control for breadcrumb.

Page Design
Consistent Layout using Master Pages
Themes for Consistent Appearance
New Code-Behind Model for Pages - Partial Classes.(clean code).
User-Customizable Web Pages - Web Parts.
New Caching Features - Configurable thru web.config,Sql server dependent cache.
Enhanced Client-Side Functionality - Ajax and Cross page posting.

Controls
Enhancements: support adaptive rendering,Themes,group validator controls.
New Controls:GridView, Treeview, SiteMappath,Login controls,

Data
Data Source Controls:encapsulate connections, query commands, and parameters into a single control,
Connection String Storage:To enhance Web site security, you can store connection strings in a dedicated section of the configuration file.

Security
support for authenticating and authorizing users using membership.

ASP.NET 2.0 adds quite a few new events to allow you to follow the request-processing more closely and precisely.
-PreInit,InitComplete,Preload,LoadComplete,PrerenderComplete.

---------- Framework 2.0

Application domains : Support is provided for unloading an application domain.
Anonymous methods
Generics
Partial classes
SQL Server integration - now Host CLR.
64-Bit support
Iterators: Iterators enable you to use foreach loops on your own custom types.


http://msdn.microsoft.com/en-us/library/fa1h9d0d(VS.85).aspx
http://www.wrox.com/WileyCDA/Section/What-s-New-in-the-NET-Framework-2-0.id-290323.html