Saturday, July 13, 2013

Differences between bitwise OR and Logical OR in C#


Difference between bitwise OR and Logical OR in C#

Laugh between | and ||

Most of time we use bitwise OR rather than logical OR. But there is a very small thing which can sleep our eyes. Recently I was using and surprised in one business logic which was going fail. Then I relies it's been serious to use it.


            int a1 = 0, b1 = 0;

            bool x = (a1 == 0 || ++b1 != 0);/* here b1 is still 0. the '++b1 != 0' was not evaluated */

            Console.Write(x);/* True*/
            Console.Write(a1);/* 0 */
            Console.Write(b1);/* 0 */

            int a2 = 0, b2 = 0;

            bool y = (a2 == 0 | ++b2 != 0);/* here b2 is 1. the '++b2 != 0' was evaluated. */

            Console.Write(y);/* True */
            Console.Write(a2);/* 0 */
            Console.Write(b2);/* 1 */

I hope it helps you somewhere.


Sunday, July 7, 2013

Differences between Aggregation and Composition

UML diagram in Software development shows complete system by using its notation. Those are classes, properties, operation and relationships (Instance and Class Level). The most interesting notations in UML are relationship. Relationship is link between entities. There is a small extent between Aggregation and Composition in instance level relationship.

Association: It lines between two entities.

Aggregation: It shows “Has a” relationship. It is more specific than association. It is used when one part is container of other side. But contains classes do not have a strong dependency on the container. Means when container is disposed then contents are not destroyed.

Notation- in UML diagram aggregation is represented by hollow diamond shape.


Example - A Student and a Course are having an Aggregation relationship. 

    public class Student
    {
        private Course course;
        public Student(Course c)
        {
            this.course = c;
        }
    }
    public class Course
    {


    }

Composition: It shows “Owns a” relationship. It is even more specific than aggregation. It gives a high dependency on container. Where container is being disposed then contains classes must be destroyed.

Notation- in UML diagram Composition is represented by filled diamond shape.


Example - A Car and an Engine are having a Composition relationship. 

      public class Car
    {
        private Engine engine;
        public Car()
        {
            engine = new Engine();
        }
    }

    public class Engine
    {

    }

Now we are in differentiation in aggregation and composition- the relationship in association is object level. It shows container and part. Aggregation has soft relationship but composition is based on strong and believes to destroy its complete parts.