March 26, 2013


Constructors enable the programmer to set default values, limit instantiation.

A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values

Constructors are mainly used to initialize private fields of the class while creating an instance for the class.

When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null. 
Types of constructors:

Default Constructor

A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.
                If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

For example, an int is initialized to 0. For more information on default values

Private Constructors:

A private constructor is a special instance constructor. It is generally used in classes that contain static members only.

 If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. 

For example:

The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. 

However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

When class contains a private constructor then we cannot create a instance of that class.

Instance Constructors:

Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class.

 To initialize a static class, or static variables in a non-static class, you must define a static constructor
 class CoOrds
    public int x, y;

    // constructor 
    public CoOrds()
        x = 0;
        y = 0;

Static Constructor:

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. 

It is called automatically before the first instance is created or any static members are referenced.

Static constructors are invoked only once and that is before creation of first instance of 
the class.

class SimpleClass
    // Static variable that must be initialized at run time. 
    static readonly long baseline;
    // Static constructor is called at most one time, before any 
    // instance constructor is invoked or member is accessed. 
    static SimpleClass()
        baseline = DateTime.Now.Ticks;

Static constructors have the following properties:

C  A static constructor does not take access modifiers or have parameters.

C  A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

C  A static constructor cannot be called directly.

C  The user has no control on when the static constructor is executed in the program.

Copy Constructor:

Unlike some languages, C# does not provide a copy constructor. 

If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.

A parameterized constructor that contains a parameter of same class type is called as copy constructor.

Main purpose of copy constructor is to initialize new instance to the values of an existing instance.

Difference between string.Empty , string = "" , string = null

string.Empty doesn't create an object.

on other hand "" creates a string object
as string is a reference type, For each time when you declare a string values as "" it create a new object for the same string.
while string.Empty just reference a string in memory that is created by default.

In case of string value is equal to null refers to absence of characters, NULL is NOT a value.

It is a state  indicating that the object value is unknown or nonexistent.

It is not zero or blank or an “empty string” and it does not behave like any of these values.

March 25, 2013

selectRandom rows from table

suppose we want a random records from table, we can use newID function of sql to get random records.

SELECT  top 5  AccountID  from AccountMaster

After executing this query you will each time new random rows.

March 23, 2013

Common Table Expressions

A common table expression (CTE) is a temporary named result set. CTE can be used within other DML statements like SELECT, INSERT, UPDATE, and DELETE.

It is not stored as an object and its lifetime is limited to the query.

It is defined using the WITH statement as the following example shows:

WITH EmployeeName ( EmpID, FName, LName)
SELECT EmpID, FName, LName FROM table
SELECT * FROM EmployeeName

A CTE can be used in place of a view in some instances.

Constants and Read Only

You set a value to const member whose value will not change during execution of program
You set a value to readonly  member whose value will change in constructor only
You can not change a value once assigned
Only in constructor
Set at compilation time
Set during runtime

March 8, 2013

Generate Script of stored procedure using Cursor

Result set will contain changes done on date 07/03/2013  and below cursor will generate script of all the stored procedure in result set


DECLARE @Objectid INT,
        @Spname   NVARCHAR(80),
        @Message  VARCHAR(5000)
  SELECT Name AS SPName,
    FROM sys.objects
   WHERE Type = 'p'
         AND CONVERT(VARCHAR(10), Modify_date, 112) = '20130307'
   ORDER BY Modify_date DESC

OPEN SpBackupCursor

FETCH NEXT FROM SpBackupCursor INTO @Spname, @Objectid

      PRINT '  Go '

      EXEC Sp_helptext

      -- Get the next vendor.
      FETCH NEXT FROM SpBackupCursor INTO @Spname, @Objectid

CLOSE SpBackupCursor;

DEALLOCATE SpBackupCursor;