Saturday, 22 February 2014

covariance and contra variance in C#.

Covariance:-
Suppose  S is subclasses of class A, type X is covariant if X<S> allows a reference conversion to
X<A>.
In other words, type MyClass<T> is covariant if the following is legal:
MyClass<int> a = someValue;
MyClass<object> o = a;


Tuesday, 18 February 2014

Why we used two interface IEnumerator and IEnumerable in Collection?

public interface IEnumerator            // provide structure for traversal in collection.
{
bool MoveNext();
object Current { get; }
void Reset();
}

public interface IEnumerable     // access the IEnumerator instant  for travelsal operation.
{
IEnumerator GetEnumerator();
}
question is that why we used two different interface for traversal in collection.

IEnumerable provides flexibility  in the iteration logic can be farmed off to another class. Moreover, it means that several consumers can enumerate the collection at once without interfering with each other.

Take example: IEnumerator is like a book. IEnumerable is like a reader. You can have many readers reading the same book, and each of them on a different page.

Two main Difference :

1. IEnumerable is Sugar-coated internally to IEnumerator . But IEnumerable has easy syntax compare to
    IEnumerator for iteration of list.

     IEnumerable<int> ie = (IEnumerable<int>)ls;  
            foreach (var item in ie)      //IEnumerable used the foreach syntax and it is easy to write.
            {
                Console.WriteLine(item);
            }
    if you use IEnumerator then

     IEnumerator<int> iet = ls.GetEnumerator();
            while (iet.MoveNext())                 // manually do the iteration
            {
                Console.WriteLine(iet.Current);
           }
2. IEnurator preserve state of Object which are using for iteration. But IEnumerable is not preserve state of       Object(means if you are inside of foreach statement and you called other function then the state of object       which are currently display are not presrve in calling function).

For More  Click Here

Sunday, 29 December 2013

Different between delegate and event.

Delegate is function pointer. It is encapsulated function name.It is point to function with same signature of delegate.Multicast delegate is used for broadcast mechanism. One delegate is refer to multiple function and called all method simultaneously.
Event is used when some action happen when activity is perform in GUI. Activity is like button click,mouse hover ect. When activity is perform then associate action(delegate) is called and that delegate is called methods which are associated this delegate.
So, we can conclude that the delegate is encapsulate the methods and event is encapsulate the the delegate.

Saturday, 28 December 2013

Why we used Delegate?

A delegate can be seen as a placeholder for a/some method(s). It is function pointer.
By defining a delegate, you are saying to the user of your class "Please feel free to put any method that match this signature here and it will be called each time my delegate is called".
Why we used delegate? for ansnwer read article this.

Wednesday, 25 December 2013

What thing you have to remeber while dealing with static member?

A static member cannot be marked as override, virtual, or abstract.

Take this example

class  Base
    {
        public static virtual  void  fun()
        {
            Console.WriteLine("Base");
        }
     }
  class child : Base
    {
     public   override void fun()  //error: Static methods never be override
        {
            Console.WriteLine("child");
        }
       }

Monday, 23 December 2013

What should be remember while dealing with Protected data member in child class?

I give one example .

class parent
   {
       protected int a;
   }
   class child : parent
   {
     
       void fun()
       {
           parent p = new parent();
           p.a = 10; //Error Cannot access protected member through parent object
           a = 10; //NO error .We can access protected member through inheritance hierarchy.
       }
   }

What thing you have to remember dealing with Static Constructor?



  •  Static constructor don't have any parameter.
  •  Static constructor don't have any access specifier because that constructor is never called from any other code so  that there is no meaning of access specifier.
  •  it is possible to define static constructor and zero parameter instance constructor defined in same class. although the parameter are identical, 
  •    there is no conflict because static constructor are called when class is loaded and instance constructor is loaded when instance is created.