Friday, November 11, 2016

Some Questions OOPS

1. Will the following code compile?
 class Base
        {
            public Base(string str)
            {
                Console.WriteLine(str);
            }
        }
        class Child : Base
        {
            public Child()
            {
                Console.WriteLine("I am a child class constructor");
            }
            public static void Main()
            {
                Child CC = new Child();
            }
        }

No, It will not compile because Base does not have parameter less constructor. And child class have one constructor without parameter so it will try to call the same in base class. Although class automatically provide default parameter less constructor if there is no constructor define. But in this case one base constructor with parameter defined.

Need following correction

class Base
        {
            public Base(string str)
            {
                Console.WriteLine(str);
            }
        }
        class Child : Base
        {
            public Child():base("Hello")
            {
                Console.WriteLine("I am a child class constructor");
            }
            public static void Main()
            {
                Child CC = new Child();
            }
        }

2. Can we use access specifier for static constructor
No, Static constructor are not allowed for static constructor.

3.Give 2 scenarios where static constructors can be used?
1. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

4. What happens if a static constructor throws an exception?
If static constructor will thorw an error, than that class will not load.

5. 

public class Parent
    {      
        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }
    public class Child : Parent
    {        
        public new void print()
        {
            base.print();
            Console.WriteLine("I'm a Child Class.");
        }
    }

    public class MainShow
    {
        public void Show()
        {
            Child child = new Child();
            child.print();//I'm a Parent Class.  I'm a Child Class.
            ((Parent)child).print();//"I'm a Parent Class.                                    
        }
    }

6. Will it compile?

    class Base
    {
        void Print();
    }

    class Child
    {
        public void Print()
        {
            Console.WriteLine("Hello");
        }
    }

    class MainClass
    {
        public void Show()
        {
            Child objChild = new Child();
            objChild.Print();
        }        
    }

Output : It will not compile : 
Error : Base.Print() must declare a body because it is not marked abstract, extern or partial.

7. Structs are not reference types. Can structs have constructors?
Yes, even though Structs are not reference types, structs can have constructors.

8. We cannot create instances of static classes. Can we have constructors for static classes?
Yes, static classes can also have constructors. But is should have static constructor only.

9. Can a child class call the constructor of a base class?
Yes, a child class can call the constructor of a base class by using the base keyword as shown in the example below.

10. An abstract class cannot be a sealed class. I.e. the following declaration is incorrect.

11. Declaration of abstract methods are only allowed in abstract classes.

12. An abstract method cannot be private.

13. The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

14. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.

//Incorrect
public abstract virtual int Show();

15. An abstract member cannot be static.

//Incorrect
public abstract  static void Show();

16.

(1) Why is this allowed?
BaseClass b = new ChildClassA(); 
ChildClassA c = (ChildClassA)b


(2) Why is this not allowed?
ChildClassA c = (ChildClassA)new BaseClass(); 
BaseClass b = (BaseClass)c;


To explain, let's use some more real world names for your example classes:
class Animal
{
}

class Dog : Animal
{
}

class Cat : Animal
{
}

So for your example (1):
Animal b = new Dog(); 
Dog c = (Dog)b

This is true because all Dogs are Animals and your Animal b is actually a dog so the cast is successful.

For your example (2):
Dog c = (Dog)new Animal(); 
Animal b = (Animal)c;

This is impossible because you are assigning an Animal object to a Dog, but you know that not all animals are Dogs, some animals are cats.

And for examples (3) & (4):
Dog c = new Dog(); 
Animal b = (Animal)c;

This is the same as your example 1 above. All dogs are animals, so any dog can be classified as an animal and cast (in fact you don't need the cast, there would be an implicit cast and you can write it as Animal b = c;

16.
 public class Baser
    {

        public Baser(int a)
        {
            Console.WriteLine(a);
        }
    }

    public class Deriver : Baser
    {

        public Deriver(int a, int b) : base(Processor(a, b))
        {
        }

        public static int Processor(int a, int b)
        {
            Console.WriteLine(b);
            return a;
        }
    }

    public class MainClass
    {
        public void Show()
        {
            Deriver objDeriver = new Deriver(10, 20);
            //Output 20 10
            //
        }
    }

Reference

  class BaseRef
    {            
        public void Display()
        {
            List objLst = new List();
            List objLst2 = objLst;

            objLst.Add(1);
            objLst.Add(2);

            ListByVal(objLst);

            Console.WriteLine("Count " + objLst.Count);// 3
            Console.WriteLine("First Val " + objLst[0]);//1
            Console.WriteLine(objLst == objLst2);// true

            ListByRef(ref objLst);

            Console.WriteLine("Count " + objLst.Count); // 2
            Console.WriteLine("First Val " + objLst[0]);// 3
            Console.WriteLine(objLst == objLst2);// false

        }

        private void ListByRef(ref List objLst)
        {
            objLst.Add(5);

            objLst = new List();
            objLst.Add(3);
            objLst.Add(4);
        }

        private void ListByVal(List objLst)
        {
            objLst.Add(5);

            objLst = new List();
            objLst.Add(3);
            objLst.Add(4);
        }
    }

    public class MainClass1
    {
        public void Show()
        {
            BaseRef objBase = new BaseRef();
            objBase.Display();                      
        }
    }

Override

    class Base1:IDisposable
    {    
        public void Dispose()
        {
            Console.WriteLine("Base Dispose");
        }

        public virtual void Display()
        {
            Console.WriteLine("Base Display");
        }
    }


    class Child1 :Base1,IDisposable
    {    
        public new void Dispose()
        {
            Console.WriteLine("Child Dispose");
        }

        public override void Display()
        {
            Console.WriteLine("Child Display");
        }
    }


    public class MainClass1
    {
        public void Show()
        {
            Base1 objBase = new Child1();
            objBase.Display();//Child Display

            objBase.Dispose();//Base Dispose

            ((IDisposable)objBase).Dispose();// Child Dispose

            IDisposable objDisp = objBase;
            objDisp.Dispose();//Child Dispose
        }
    }

Constructor calling Sequence

class Base
    {

        /*public */
        static Base() //Access specifier not allowed for static constructor
        {
            Console.WriteLine("Base Static");
        }

        public Base()
        {
            Console.WriteLine("Base Constructor");
        }

    }


    class Child :Base
    {

        /*public */
        static Child() //Access specifier not allowed for static constructor
        {
            Console.WriteLine("Child Static");
        }

        public Child()
        {
            Console.WriteLine("Child Constructor");
        }

    }


    public class MainClass
    {
        public void Show()
        {
            Child objChild = new Child();
            //Output
            // Child Static
            //Base Static
            //Base Constructor
            //Child Constructor
        }
    }

Thursday, November 3, 2016

Dependencies properties


WPF vastly uses dependency properties internally. For example button’s background property is backed by a dependency property called BackgroundProperty.

Dependency property’s name always ends with property.

Dependency properties are always declared as static and read-only because it should be available to all the controls hence it should be shared and therefore it should always static.

It should be read-only because it’s value should not be changed after initialization.
Data binding can be done with dependency properties only.

Dependency properties are special properties which provide change notification.


Why We Need Dependency Properties


Dependency property gives you all kinds of benefits when you use it in your application. Dependency Property can used over a CLR property in the following scenarios −
If you want to set the style
If you want data binding
If you want to set with a resource (a static or a dynamic resource)
If you want to support animation

Basically, Dependency Properties offer a lot of functionalities that you won’t get by using a CLR property.

The main difference between dependency properties and other CLR properties are listed below −


CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.


Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well.

Routed Event

A routed event is special type of event which can invoke handlers on multiple listeners in an element tree rather than just the object that raised the event.

Routed have three main routing strategies

1.       Direct Event
2.       Bubbling Event
3.       Tunneling Event
Direct event is normal CLR event.
Bubbling event travels up in visual tree hierarchy.
Tunneling event travels down in visual tree hierarchy.


The difference between a bubbling and a tunneling event is that a tunneling event will always start with a preview. Ex.. previewkeydown, previewkeyup. We can create our routed event.

Followers

Link