Friday, September 16, 2011

Difference between Int32.Parse and Convert.ToInt32

Both function use for string format to integer value.

There are some cases

1) If string is null Parse throws ArugmentNullException
2) If String it not compatible to int it throws Format Exception
3) If value is less than Minimum value or greater than Maximum value it will throws Overflow Exception

While in case of Contvert.ToInt32. This function is a wrapper of Int32.Parse. It throws the same exception except in case of null string it returns zero(0).

Example
string s1 = "20";
string s2 = "25.50";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";

int result;
bool success;

result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException


result = Convert.ToInt32(s1); //-- 25
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException

Int32.TryParse doesn't throws any exception instead it returns zero if value is not compatible. and return false.

success = Int32.TryParse(s1, out result); //-- success => true; result => 25
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0

Friday, September 9, 2011

Locking in SQL Server

In multi user environment SQL Server use lock to prevent conflict modification. This is purely logical no hardware is needed for that.

There are different types of lock available in SQL Server.

1) Shared Lock :- In shared lock other process can not modify the data they can read only. Shared lock used for operations that do not change or modify the data. Select statement is an example of shared lock. Shared lock are held on data under the pessimistic concurrency level.Two shared lock are compatible with each other.

2)Update Lock :- SQL Server use the Update Lock when SQL Server intends to modify the data. It later promote Update Lock to Exclusive lock when actually making the changes.Update lock are compatible with shared lock.Basically to avoid deadlock update lock has used.

3) Exclusive (X) :- Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.

4) Intent :- Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

5) Schema :- Used when an operation dependent on the schema of a table is executing. The types of schema locks are schema modification (Sch-M) and schema stability (Sch-S).

6) Bulk Update (BU) :- Used when bulk-copying data into a table and the TABLOCK hint is specified.


Friday, September 2, 2011

Static Class

Static class used to define the information that is object independent like company info. Static class contain only static methods and properties. Instance constructor can not be created inside static class. Although a static constructor can be defined there.

Properties of static class

1) Static class are sealed they can not be inherited since there is no constructor. Like a class which contain a private constructor.
Static class can not inherit another class nor simple nor static.
2) They can not be instantiated.
3)They are loaded when program or namespace containing the static class load.
4) They can not have non static methods or variables.
5) They can have only static constructor if needed.
6) Benefit of using static class, compiler guarantee this thing that class does not have any instance.
7) Static class make implementation simple and faster since there is no object needed to call the methods. A good example of static class is Math class that contain all math functions like sin,cosine.

Followers

Link