Sunday, December 16, 2007

Expression Trees

Hi

C# 3.0 introduced Lambda Expressions.
These are one of the building blocks of the LINQ.
Expression is a syntactic feature. uses the anonymous methods and the delegates (generic delegates).
It is a simplified syntax over using the generic delegates.

The general form of a Lambda Expression is

parms => function body

Consider following example.

p => ++p;

This Lambda Expression translates to

Func <<int,int>> incr = delegate (int p)
{
return ++p;
}

One good thing about these Lambda expressions is that one can tell compiler to generate an expression tree for a Lambda expression instead of a IL method ( in above example the Func delegate tells the compiler to generate the IL method).

Above expression can be rewritten as follows

Expression<<<<Func<<int,int>>>> exprIncr =
delegate (int p)
{
return ++p;
}

or

Expression<<<<Func<<int,int>>>> exprIncr = p=> ++p;

both code snippets above are same.

Expression trees are nothing but a binary tree which represents an expression. ( something like a tree for an arithmetic expression like a+b).

Following article goes in depth for expression trees.
http://courseweb.sp.cs.cmu.edu/~cs200/lecture18/lecture18.html.

Following post gives an overview of Lambda expressions and there genesis starting from .Net 1.0 delegates.

http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees


Thanks

LINQ to SQL

Hi

One of the cool features of C# 3.0 is the LINQ.
This technology has the capability of changing the way tiered applications are written.

This is a great article from Microsoft this topic.
http://msdn2.microsoft.com/en-us/library/bb425822.aspx

Thanks

Saturday, December 15, 2007

Object Initializers in C# 3.0

Hi

C# 3.0 introduced something called as object initializers.

Consider following class

class Person
{
public int Age;
public string Name;
.....
}

One can instantiate this class as

Person firstPerson = new Person();
firstPerson.Age =10;
firstPerson.Name = "Bal";

We can also use the parametrized constructors for initializing attributes of a class.

With C# 3.0 we can create the Person object as

Person secondPerson = new Person{ Age = 10, Name ="Bandu" };

This is not same as creating the object and setting its values.

The expression is evaluated right to left and hence a temporary object is created which is finally assigned to the secondPerson.

One good thing about this expression is this is an atomic expression.
Means we don't have to specify any lock kind of construct when using this in multi threaded programs.


Thanks

Tuesday, November 20, 2007

Creating Services

As we know services play an important role in applications.
.Net simplified the development/deployment of windows services.

To create a service in .Net you just need to create an installer class and a class inherited from ServiceBase. The class derived from ServiceBase represents the service code that actually executes.

Also the service development is C++ is also not that tedious.
Following article explains a simple approach to create a simple service using C++ (C).
http://www.devx.com/cplus/Article/9857

Thanks
Prasad

Sunday, November 18, 2007

Garbage Collection in .Net

Hi
Garbage collection (GC) is one of the cool features of .Net framework.

This feature is exploited in following 2 part article by Jeffrey Richter.

http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

Thanks
Prasad

Thursday, November 15, 2007

CreateProcessAsUser

Some times we need to spawn a child process. The trick however being it has to be execute with some different user account. This situation can arise in some ASP.Net applications as well.

It is not always possible to have the password of the user account under which we want to spawn the process.
In such cases .Net's standard System.diagnostics.Process.Start doesn't work.

Here comes WIN32 for our help.

CreateProcessAsUser Win32 API is most suitable for such situations.
Following are the links where this scenario is very well explained.

http://support.microsoft.com/default.aspx?scid=kb;EN-US;889251

http://odetocode.com/Blogs/scott/archive/2004/10/28/602.aspx


Thanks
Prasad Pimparkar