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
     }