Tuesday, June 17, 2008

Operators in C#

It is important to know that C# and java support the rules of IEEE, means if you have 10.5/0 you won't get an error. It will be infinity or if you have 0.0/0.0 the result will be not a number. But if you have 10/0 then you will got an overflow error.

As you all know there are different kinds of operators. 
 ·         Relational Operators such as ==,!=,>,>=,<,<= which are used between two operands(except !=) and the result is a Boolean.
 ·         Logical operators such as &,,&&, that are used between two Boolean operands and return a boolean. It is good to point that && and  make a short circuit, means if the first operand is false the && operator returns false without checking the other operand. But if the first operand is true  operator will return true without checking the other one.
 ·         Arithmetic operators such as +,-,*,%. No need to describe them more as you are familiar with 
 
Besides the other operators that we don't describe them here, there are some important ones that are basic in C#.

++ Increment operator, incrementing the value by 1 (i++ is the same as i=i+1)

— Decrement operator, decrementing the value by 1 (i— is the same as i=i-1)

+= Increment assignment operator (i+=5 is the same as i=i+5)

-= Decrement assignment operator (i-=5 is the same as i=i-5)

== and != Equality and Inequality operator

% Modulus (remainder) operator. (17%5 equals 2)

Thanks to Azadeh for this post

Sunday, June 8, 2008

Evaluate your code by Code Analysis

We may write lines and lines of code but never check our code to see whether we follow up the best practice for coding. Sometimes, we make big mistakes in our codes it may not appear now but in future when change requests come. It reveals our code was not that much good we thought it was. So how can a programmer be confident about his code? The only way is that you need to review your code but in reality being overloaded we never have this chance. Nevertheless, we need code checking to be automatic and Code Analysis is a tool absolutely free with Visual studio .Net helps us to check our code in terms of security, design, maintainability, reliability and a lot more.

How to configure Code Analysis?

Open your project in visual studio then right click on your project and select properties. Then select code analysis tab. And you need to check "Enable Code Analysis". I highly recommend taking a look at different Rules that you can check. For example if you have different naming convention you can unselect that.

Then all you need in solution explorer right click on your project and select Run Code Analysis. It will show you some warning for your code.

Take a look at this picture

For simple code that I wrote code analysis could find some issues with my code. One of them says for enum value you need to have a zero value which is part of Microsoft best practice.