Pages

Thursday, June 12, 2008

Call with variable arguments

Sometimes we may require a variable number of arguments to be passed to a function. For example we may require a sum function which calculates the total of the numbers passed to it no matter how many numbers are passed.

In C# we can use the Params keyword and pass variable no of arguments to a function. It's much like using ParamArray in VisualBasic language. The syntax of params arguments is:
params datatype[] argument name

static void ListArguments (params object[] arguments)
{
foreach (object argument in arguments)
{
Console.WriteLine (argument);
}
}

public static void Main( )
{
ListArguments ("Arguments: ", DateTime.Now, 3.14f);
}

Saturday, June 7, 2008

IS vs AS

Use the as operator when the normal program flow is to test for the interface and—immediately—cast the object for access to the interface members.

Use the is operator when the normal program flow is to test for the interface; but, interface member access is either delayed or does not occur at all.

Calculate Fibonacci number

static int Fibonacci (int x)
{
Console.WriteLine ("x = {0}", x);
if (x <= 1)
{
return 1;
}
return Fibonacci (x-1) + Fibonacci (x-2);
}

static void Main( )
{
Console.WriteLine ("Fibonacci no. = {0}", Fibonacci (5));
Console.ReadKey();
}

Thursday, June 5, 2008

Native Image Generator Tool

.NET programming tool (Ngen.exe) for compiling an assembly to native machine code and installing it in the local assembly cache. Rather than the MSIL assembly, the native image is used for assembly access during execution. The Common Language Runtime (CLR) reverts to using the MSIL assembly if the native image is removed. Native images load and execute faster than MSIL assemblies which must be Just-In-Time (JIT) compiled by the CLR. Known as pre-JITing, using this tool to create a native image file makes JIT-compiling unnecessary.

Set a Database to Single-user Mode

Single-user mode specifies that only one user at a time can access the database and is generally used for maintenance actions.

The database remains in single-user mode even if the user that set the option logs off. At that point, a different user, but only one, can connect to the database.

To set a database to single-user mode
--> In Object Explorer, connect to an instance of the SQL Server 2005 Database Engine, and then expand that instance.

--> Right-click the database to change, and then click Properties.

--> In the Database Properties dialog box, click the Options page.

--> From the Restrict Access option, select Single.

--> If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.

Wednesday, June 4, 2008

Join algorithms

Three fundamental algorithms exist for performing a join operation.

Nested loops
Use of nested loops produces the simplest join-algorithm. For each tuple in the outer join relation, the system scans the entire inner-join relation and appends any tuples that match the join-condition to the result set. Naturally, this algorithm performs poorly with large join-relations: inner or outer or both. An index on columns in the inner relation in the join-predicate can enhance performance.

The "block nested loops" (BNL) approach offers a refinement to this technique: for every block in the outer relation, the system scans the entire inner relation. For each match between the current inner tuple and one of the tuples in the current block of the outer relation, the system adds a tuple to the join result-set. This variant means doing more computation for each tuple of the inner relation, but far fewer scans of the inner relation.


Merge join
If both join relations come in order, sorted by the join attribute(s), the system can perform the join trivially, thus:

Consider the current "group" of tuples from the inner relation; a group consists of a set of contiguous tuples in the inner relation with the same value in the join attribute.
For each matching tuple in the current inner group, add a tuple to the join result. Once the inner group has been exhausted, advance both the inner and outer scans to the next group.
Merge joins offer one reason why many optimizers keep track of the sort order produced by query plan operators—if one or both input relations to a merge join arrives already sorted on the join attribute, the system need not perform an additional sort. Otherwise, the DBMS will need to perform the sort, usually using an external sort to avoid consuming too much memory.

Hash join

A hash join algorithm can produce equi-joins. The database system pre-forms access to the tables concerned by building hash tables on the join-attributes. The lookup in hash tables operates much faster than through index trees. However, one can compare hashed values only for equality, not for other relationships.