Membership Service in ASP.Net 2.0

Membership Service in ASP.Net 2.0
Published on http://asp.net on 10/16/2008

Tuesday, April 16, 2013

Handling overflow exceptions: Checked/Unchecked


In .Net, when two numbers are added, the corresponding IL code that is generated has two representation and by default .add call is executed.

1. add
2. add.ovf

Because .add is called, the overflows go undetected and might cause the applications built around it to behave erratically. In Order to ensure the overflow check, use of "checked" keyword could be employed

e.g.

Without "checked"


                byte b1 = 100;
                byte b2 = (byte)(b1 + 200);
                Console.WriteLine(b2.ToString());
                Console.ReadKey();

on executing the above piece of the code output would be : 44
Since byte can be b/w 0 and 255. 300-256 =44


       
With "checked"



               byte b1 = 100;
                byte b2 = checked((byte)(b1 + 200));
                Console.WriteLine(b2.ToString());
                Console.ReadKey();

in this case, overflow exception will be thrown.

Similarly, the "unchecked" keyword can be used to perform the reciprocal of "checked" operation.

Likewise as explained above, the OPCodes for Subtract, Multiply, Divide also exist.

Monday, April 15, 2013

"is" vs "as" keywords in .Net

Coming soon....

String vs string

Developers at time use "String" and sometimes "string" and often get posed with the question as to which is correct.

Answer : Either of it could be used as base type for string is System.String (CTS Data Type), string is specific to C#

== vs .Equals()

In .Net you could use "==" or .Equals()

== will return true if the reference types are pointing to object in Memory.

.Equals will perform case sensitive and character by character equality check.


            StringBuilder a = new StringBuilder("anubhav");
            StringBuilder b = new StringBuilder("anubhav");

            if (a == b)
                Console.WriteLine("a==b is true");
            else
                Console.WriteLine("a==b is false");

            if(a.Equals(b))
                Console.WriteLine("a.equals(b) is true");
            else
                Console.WriteLine("a.equals(b) is false");


Output :
a==b is false
a.equals(b) is true