Thursday, November 12, 2009

Difference between const and readonly

The readonly keyword is different from the const keyword.

A const field can only be initialized at the time of declaration. It specifies that the value of the field or the local variable cannot be modified.
A readonly field or variable can be initialized either at the declaration or in a constructor in the same class. Therefore, readonly variable or fields can have different values depending on the constructor used. A static readonly field can be set at run time, so it can have a different value for different executions of the program. However, the value of a const field is set to a compile time constant.

A const field is a compile-time constant.
A readonly field is a runtime constant.

using System;

public class Test
{
public const int rows = 10; // Initialize a const field
public const int seats = rows + 25;
public const double size = 5.0, width = 72.5, height = 44.45;

public readonly int x = 30; // Initialize a readonly field
public readonly int y;

public Test()
{
y = 100; // Initialize a readonly instance field
}

public Test(int a, int b)
{
x = a;
y = b;
}
}

Wednesday, November 12, 2008

Last N rows from SQL Server Table

Retrieving last 5 rows from a table

SELECT TOP (SELECT Count(*) FROM Person) * FROM Person
Except
SELECT TOP (SELECT Count(*)-5 FROM Person) * FROM person


OR

SELECT TOP (SELECT Count(*) FROM Person) * FROM Person
WHERE PersonName NOT IN(
SELECT TOP (SELECT Count(*)- 5 FROM Person) PersonName FROM person
)

Friday, October 31, 2008

Types of Join in SQL Server

Join
By using joins, we can retrieve data from two or more tables based on logical relationships between the tables. Joins indicate how SQL Server should use data from one table to select the rows in another table. Sql server joins are used to combine result data from two or more tables.
There are three types of sql server joins.

1. Inner Join
2. Outer Join
3. Cross Joins

1. Inner Join

Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. Inner joins return rows only when there is at least one row from both tables that matches the join condition. Eg:Retrieving all rows where the student identification number is the same in both the students and courses tables.

2. Outer join
Inner joins eliminate the rows that do not match with a row from the other table. Outer joins, however, return all rows from at least one of the tables or views mentioned in the FROM clause.

There are there 3 types of outer joins.

a. Left outer join
All rows are retrieved from the left table referenced with a left outer join. If matching records are found then it will display data of right table with left table data otherwise put a null values instead of right table data.

b.Right outer join

All rows are retrieved from the right table referenced in a right outer join. If matching records are found then it will display data of left table with right table data otherwise put a null value instead of left table data.

c.Full outer join
All rows from both tables are returned in a full outer join.This joins are combination of both left outer join and right outer join.

3. Cross join

A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table.

Self-join

A table can be joined to itself in a self-join.

Wednesday, October 29, 2008

Dispose method

Dispose method of IDisposable interface performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. This method is used to explicitly release unmanaged resources in conjunction with the garbage collector.

When implementing this method, ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and object B allocates an object C, then A's Dispose implementation must call Dispose on B, which must in turn call Dispose on C. An object must also call the Dispose method of its base class if the base class implements IDisposable.
Because the Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called. Once the Dispose method has been called, it is unnecessary for the garbage collector to call the disposed object's finalizer. Dispose implementation calls the GC.SuppressFinalize method, to prevent automatic finalization.


 
// Implement IDisposable. Do not make this method virtual. A derived class should not be able to override this method.

public void Dispose()
{
// This object will be cleaned up by the Dispose method. Therefore, you should call GC.SupressFinalize to take this object off the finalization queue and prevent finalization code for this object from executing a second time.
GC.SuppressFinalize(this);
}