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.

Wednesday, October 12, 2016

ICommand and Command

1. Command are loosely typed events.
2. ICommand interface is used to implement command in MVVM
3. ICommand interface exist in System.Window.Input
4. ICommand interface contains two methods Execute, Canexecute and one event handler CanExecuteChanged.

To bind a command of a button you need to bind a property of type  ICommand. An ICommand contains
  1. event EventHandler CanExecuteChanged;    
  2. bool CanExecute(object parameter);    
  3. void Execute(object parameter);    
CanExecuteChanged is invoked when changes occur that can change whether or not the command can be executed.

CanExecute will determine whether the command can be executed or not. If it returns false the button will be disabled on the interface.

Execute runs the command logic.

With a simple implementation of ICommand I can create the following:
  1. public class NormalCommand : ICommand    
  2. {    
  3.     public event EventHandler CanExecuteChanged;    
  4.      
  5.     public bool CanExecute(object parameter)    
  6.     {    
  7.           
  8.     }    
  9.      
  10.     public void Execute(object parameter)    
  11.     {    
  12.          
  13.     }    
  14. }   
However this does not allow me to have a different logic to my CanExecute or Execute. For each command I would need to implement a new class. To solve that problem there is the RelayCommandimplementation that is a command that can be instantiated passing the actions to be executed as in the following:
  1. public class RelayCommand : ICommand    
  2. {    
  3.     private Action<object> execute;    
  4.     private Func<objectbool> canExecute;    
  5.      
  6.     public event EventHandler CanExecuteChanged    
  7.     {    
  8.         add { CommandManager.RequerySuggested += value; }    
  9.         remove { CommandManager.RequerySuggested -= value; }    
  10.     }    
  11.      
  12.     public RelayCommand(Action<object> execute, Func<objectbool> canExecute = null)    
  13.     {    
  14.         this.execute = execute;    
  15.         this.canExecute = canExecute;    
  16.     }    
  17.      
  18.     public bool CanExecute(object parameter)    
  19.     {    
  20.         return this.canExecute == null || this.canExecute(parameter);    
  21.     }    
  22.      
  23.     public void Execute(object parameter)    
  24.     {    
  25.         this.execute(parameter);    
  26.     }    
  27. }  
With this implementation I can specify what I want to execute when I create the command, so I don't need to implement a new class for each different action I want to take. Then it could be called using the following:
  1. var cmd1 = new RelayCommand(o => { /* do something 1 */ }, o => true);    
  2. var cmd2 = new RelayCommand(o => { /* do something 2 */ }, o => true);   
The CommandManager.RequerySuggested handles events when something in the interface suggests that a requery should happen. If your ICommand adds the handlers to it then it will automatically update UI elements when the screen executes some actions. (For example, lose focus on a TextBox.)

Friday, August 5, 2016

High –Level Design And Low – Level Design

High – level Design gives the overall System Design in terms of Functional Architecture and Database design. This is very useful for the developers to understand the flow of the system. In this phase design team, review team (testers) and customers plays a major role. For this the entry criteria are the requirement document that is SRS. And the exit criteria will be HLD, projects standards, the functional design documents, and the database design document.

Low – Level Design (LLD)

During the detailed phase, the view of the application developed during the high level design is broken down into modules and programs. Logic design is done for every program and then documented as program specifications. For every program, a unit test plan is created.

The entry criteria for this will be the HLD document. And the exit criteria will the program specification and unit test plan (LLD).

Wednesday, July 20, 2016

GBL-Interview

1. What is difference between Background worker and task?
2. What is composite pattern?
3. Have you use prism framework?
4. What is critical section?
5. What is High Level Architecture and How did you use it?
6. What is event tunnling?
7. What is mocking?
8. WPF object hierarchy?
9. What is IOC(Inversion of Control)?
10. What is generic?
11. What is Background worker?

Thursday, July 14, 2016

What is Agile?


Agile Software development approach says that don't complete the project in one go and at the end go to client for approval rather than complete a basic version with some features first and show it to client and get approval and his feedback. Than in second version add some more features and show them to client.

What is Sprint duration?
Sprint duration stands for in how many days you release a new version of application.
We usually release every week i.e on Friday.

Friday, June 17, 2016

Difference Between var and dynamic

1. Var is introduced in 3.0 while dynamic introduced in 4.0.
2. Var is statically typed means type is decided at compile time. Dynamic is dynamically typed means type is decided at runtime.
3. Error caught at compile time in case of var while with dynamic error caught at runtime.
4.Need to initialize at compile time when you are using var. In case of dynamic no need to initialize at compile time.
var obj;
var obj1=null;
Above statement give compile error. While below statement will not give any error.
dynamic obj;
dynamic obj1=null;
5. Intellisense work for var but not for dynamic.
6. Below statement will not compile
var obj =12;
obj = "Hello"
While these statements perfectly fine with dynamic.
dynamic obj =12;
obj = "Hello"
7. var can not be passed as an arguments and function can't return var type. While dynamic can be passed as an argument and function also can return dynamic.
7. var is type safe , dynamic is not.
8. var can't used as a property while dynamic can.
9.var can be use as a local variable only. Member var type not allowed. Dynamic can be use as a member variable as well as local.

Tuesday, June 14, 2016

Difference Between WPF User Control and Custom Control

User Control: When you need to put some controls together than you use user control. Example is RGB color panel. It is same as window because it has code behind file as well xaml file.
It does not support theming for consumers.It is better to stay with consumer application.

Custom Control: When you wan to extent the funtionality of a given control than use Custom Control Like Numeric Drop down is an extended custom control , Which is build by customizing text box.
It support theming for consumers. It is perfect to be 3rd party conrol.

Infogain

1. Explain design pattern
Design Pattern

2. Difference between var and dynamic
Click Here For Diff

3. Can we have static constructor in abstract class?
Ans : Yes

4. Can we have both in same class static constructor and instance constructor?
Ans : Yes

5. Difference between override and new
Click Here

6. Difference between value type and reference type
7. Is it possible int? i=null
Ans Yes

8. Difference between factory and Abstract Factory
9. Define singleton pattern

10. Where singleton pattern used in .net?
Ans Dispatcher

11. What is the use of using statement?
Ans. Call the Dispose method if object is created using using statement.

12. If any error occur during execution of using block, did dispose method would call?
Ans Yes

13. Is it possible to define two catch statement where super exception class is above sub class?
Ans : Not possible, will give compile time error

14. Implement a queue with the help of two stack

15. Can we assign base class object to child class object?
Ans : No

16. Is static variable allowed inside function C#?
Ans : No, static variable are not allowed inside function in C# while they are allowed in C and C++.

There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

Saturday, June 11, 2016

Table Variable



SQL Serve 2000 introduces a new variable named table. You can say table variable is an alternative to temporary table. Which is better in both it depends on scenario somewhere table variable is good and somewhere temporary table.
You can save record in table variable
Declaration:
DECLARE @SalaryDetail TABLE
(
  Basic int,
  PF int,
  EmpId varchar
)

Fill the table

Insert into @SalaryDetail (Basic,PF,EmpId) Select Basic,PF,EmpId from Salary.

Table variables can be used in batches, stored procedures, and user defined functions. It also can be update and delete as well.

Update @SalaryDetail Set PF=PF*8/100 where EmpIt=’2’

Delete from @SalaryDetail where EmpId=’1’
Select Top 10 from @SalaryDetail OrderBy Basic Desc
Scope: Table variable cannot use as input and output parameter in procedure or function. Just like other variable table variable also no longer exist after the procedure exits. No need to drop the table (table variable).
A user defined function can return a table variable.
Performance: A table variable will generally use fewer resources in comparison of temporary variable cause is its well-defined scope. Transactions lock the table variable at last moment so less locking time.
There is no need to recompile the stored procedure in case of table variable unlike temporary table.

Other Features:


//////////////////////////////////////////////////////////////////////////////////////
Using a temporary table inside of a stored procedure may result in additional re-compilations of the stored procedure. Table variables can often avoid this recompilation hit. For more information on why stored procedures may recompile, look at Microsoft knowledge base article 243586 (INF: Troubleshooting Stored Procedure Recompilation).
Other Features
Constraints are an excellent way to ensure the data in a table meets specific requirements, and you can use constraints with table variables. The following example ensures ProductID values in the table will be unique, and all prices are less then 10.0.
DECLARE @MyTable TABLE
(
  ProductID int UNIQUE,
  Price money CHECK(Price < 10.0)
)
You can also declare primary keys. identity columns, and default values.
UPDATE @ProductTotals
DECLARE @MyTable TABLE
(
  ProductID int IDENTITY(1,1) PRIMARY KEY,
  Name varchar(10) NOT NULL DEFAULT('Unknown')
)
So far it seems that table variables can do anything temporary tables can do within the scope of a stored procedure, batch, or UDF), but there are some drawbacks.

What is Web API?



WCF was too complicated to create REST services,  that’s why Microsoft introduced ASP.NET Web API to create RESTful services that are capable of providing fully resource oriented services for a broad range of clients including mobiles, browsers and tablets. ASP.NET Web API is a framework for building REST services easily and in a rather simple way. 

A Web API is an API over the web (HTTP) and ASP .Net Web API is a framework that is use to build HTTP Services. Those services can be used by various client including mobile, Tab, or Browser.  ASP .Net Web API is an ideal platform to build RESTful services.

Followers

Link