-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: bhavesh26
Message 1 in Discussion

 Hello All,   Last day I came across the C# inherent technique of Short 
Circuiting.What is it and how it is useful in programming in both C# and VB.Net 
explained below.   Consider the following code :   static bool DoTask1(  )
{
   return true;
} static bool DoTask2(  )
{
   return false;
} static void Main(string[] args)
{
   //it is unnecessary to execute
   //DoTask2 because DoTask1 returns
   //True, which is enough to satisfy
   //the or clause
   if (DoTask1() || DoTask2(  ))
   {
   }
  
   //it is unnecessary to execute
   //DoTask1 because DoTask2 returns 
   //False, which means the clause is
   //False
   if (DoTask2() && DoTask1(  ))
   {
   }
}   In this example, the function DoTask1 always returns true, and the function 
DoTask2 always returns false. C# uses a technique called short-circuiting, 
which means that if part of the statement makes the entire statement true or 
false, there is no need to process the rest of the statement.    For example, 
in an or operation, if at least one of the clauses is true, then the entire 
statement is true. In the example above, DoTask1 returns true; therefore, there 
is no need to execute DoTask2.    The second half of the code example contains 
an and operation. In an and operation, all clauses have to be true. If one of 
the clauses is false, there is no need to evaluate the rest of the statement.   
 In this example, DoTask2 returns false; therefore, there is no need to 
evaluate DoTask1. 
 For the VB.Net : VB does not short-circuit by default. In VB, all parts of the 
conditional statement are executed unless you use the special keywords AndAlso 
or OrElse. Both the Or clause and the And clause will execute DoTask1 and 
DoTask2 without short-circuiting. If you would like to short-circuit, you must 
replace And with the AndAlso keyword and Or with the OrElse keyword. 
What happens Inside the IL :
If you use And and Or in VB.NET instead of the short-circuiting equivalents, at 
the IL level VB executes all the functions involved in the If first, then 
compares the results. In the case of short-circuiting, VB executes one 
statement, then checks the result to see if it needs to continue. If it does 
not, it skips the rest of the statements. Otherwise it executes the second 
statement and checks again, and so on.     cheers, Bhavesh Patel, Jr. Software 
Engg, Elegant Microweb Pvt. Ltd, Ahmedabad

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to