Sunday 10 May 2015

Designing With inheritance and composition,Which one you choose?

Inheritance, it is "IS A" relationship. Example, Employee is a person. Employee is base class and person is child class.

Composition is a "HAS A" relationship. Example Bike has a tyres. Bike class has reference of tyres class.

When you used inheritance and when you used composition.
  1. The only way to get maximum advantage of both inheritance and composition in your designs is to ask yourself if you have a permanent is-a relationship. If yes then use inheritance. If not, use composition.  
    • For Example. An question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application. For example, you might think that an Employee is-a Person, when really Employee represents a role that a Person. What if the person becomes unemployed? What if the person is both an Employee and a Supervisor? Such impermanent is-a relationships should usually be modelled with composition.
    • And an Apple likely is-a Fruit, so I would be inclined to use inheritance. here no change to change is a relationship.
  2. As thinks for performance Inheritance is more costly compare to Composition. Every time you create object of child class before that parent constructor is called and memory is allocated for parent variable. Also you can not change behaviour of base class as run time.
  3. when you have need of the polymorphism then Polymorphism is achieved by inheritance.

Thursday 26 February 2015

Find MAX, MIN ,Second MAX and Second MIN value in C# LINQ

using System.IO;
using System;
using System.Linq;

class Program
{
    static void Main()
    {
       var a=new int[6]{1,10,5,7,4,3};
        int max1=a.Max();   //MAX
        int max2=a.Min();    //MIN
        int max3=a.OrderByDescending(z=>z).Skip(1).First();
                                         //Second MAX
        int max4=a.OrderBy(z=>z).Skip(1).First();
                                        //Second MIN
       
        Console.WriteLine("{0}",max1);
        Console.WriteLine("{0}",max2);
        Console.WriteLine("{0}",max3);
        Console.WriteLine("{0}",max4);
        Console.WriteLine("Hello, World!");
    }
}

Output:-
10
1
7
3

Monday 15 September 2014

Difference Between Pass by reference and pass reference Type

(1)
 void Fun(int[] Arr)
    {
        Arr[0] = 888;  // This change affects the original element.
        Arr = new int[5] {-3, -1, -2, -3, -4};   // This change is local.
                           // Because two different refernece is created 
        System.Console.WriteLine("the first element is: {0}", Arr[0]);
    }
static void Main() 
    {
        int[] arr = {1, 4, 5};
        Fun(ref arr); // Call above method
    }
In this case two reference is created. Ref1 and Ref2 are both are pointed to same object. You can change Reference point object by "New" Keyword.
(2)

 static void Fun(ref int[] Arr) {
   // Both of the following changes will affect the original variables:
    Arr[0] = 888;
     Arr = new int[5] {-3, -1, -2, -3, -4};
     System.Console.WriteLine("the first element is: {0}", pArray[0]);
   }
 static void Main() {
     int[] arr = {1, 4, 5};
      Fun(ref arr);
}
Above case only one reference is created. "New" keyword is 
change original object.





Method override interview question in C#

class myClass
    {
        public void myFun(out int a) // ERROR
        {
            a = 10;
        }
        public void myFun(ref int a)
        {
            a=20;
        }
  }
Error:- Cannot define overloaded method 'myFun' because
it differs from another method only on ref and out. And Ref and Out
used Pass by reference for parameter Passing.


You can't use the ref and out keywords for the following kinds of methods:
  • Async methods, which you define by using the async modifier.
  • Iterator methods, which include a yield return or yield break statement.
-----------------------------------------------------------------------------

class myClass
{
 public void myFun(int a)  //No Error
 {
     a = 10;
  }
 public void myFun(ref int a)
 {
    a=20;
  }
 }
-----------------------------------------------------------------------------
void myFun(object x) {}
void myFun(dynamic x) {}//No Error.



Sunday 14 September 2014

Object.gethashcode in c#

A hash code is a numeric value that is used to insert and identify an object in a hash-based collection such as the Dictionary<TKey, TValue> class, the Hashtable class, or a type derived from the DictionaryBase class. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality.

Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes.

 class Program
    {
        static void Main(string[] args)
        {
             B obj1 = new B();
            B obj2 = new B();
            Console.WriteLine(obj1==obj2);
            Console.WriteLine(obj1.Equals(obj2));
            Console.WriteLine(obj1.GetHashCode());
            Console.WriteLine(obj2.GetHashCode());
            Console.ReadLine();
        }
    }
    class B:Object
    {
        public override int GetHashCode()
        {
            return 1 ;
        }
        public override bool Equals(object obj)
        {
            return true;
        }
    }
Output:-
False
True
1
1

Here both object has same hashcode. It is not means that it is equal. but if it is two object are same then it has equal hashcode.

Wednesday 10 September 2014

Two interface have same method in C#

Interface I1
    {
        void fun();
        int fun1();
    }
Interface I2
    {
        void fun();
         string fun1();
    }
 class temp : I1, I2
    {
        void fun(){ ......}  // No compile error because two method has same signature,
                                    //Run Time is throw error.
        int fun1(){......}  // Error: we have explicit declare both methods
     }

             


Tuesday 19 August 2014

trancate string in CSS if it goes out of the parent content?

.myClass 
{
    overflow: hidden;         // hide the string if it is overflow.
    text-overflow: ellipsis;  //put three dot after trancate string.
}