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.
}

Thursday, 14 August 2014

Inline switch case in SQL

CASE ISNULL(upub.SharedBy,0) //ISNULL -if value is null then convert to 0.
       WHEN 0  // true when upub.SharedBy=0
            THEN pub.PublicationDate
            ELSE  upub.DateUpdated
 END

Tuesday, 5 August 2014

Features of Jquery

Features of Jquery 
1. One can easily provide effects and can do animations. 
2. Applying / Changing CSS. 
3. Cool plugins. 
4. Ajax support 
5. DOM selection events 
6. Event Handling 


and many more other feature provide by jquery.

difference between e.preventDefault(); and return false

The difference is that preventDefault will only prevent the default event action to occur, i.e. a page redirect on a link click, a form submission, etc. and return false will also stop the event flow.

Tuesday, 1 July 2014

Attach data of any type to DOM elements in Jquery.

The .data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.


$( "div" ).data( "foo", 52 );
$( "div" ).data( "bar", { myType: "test", count: 40 } );
$( "div" ).data( { baz: [ 1, 2, 3 ] } );
$( "div" ).data( "foo" ); // 52
$( "div" ).data(); // { foo: 52,  // bar:{ myType: "test", count: 40 }, // baz: [ 1, 2, 3 ] } Example:
<body>
<div>
The values stored were
<span></span>
and
<span></span>
</div>
<script>
$( "div" ).data( "test", { first: 16, last: "pizza!" } );
$( "span:first" ).text( $( "div" ).data( "test" ).first );
$( "span:last" ).text( $( "div" ).data( "test" ).last );
</script>
</body>
Output:The values stored were 16 and pizza!

Monday, 30 June 2014

How to redirect one page to other in Asp.Net MVC 4?



window.location.href example: window.location is object that holds all information about current document location (host, href, port, protocol etc.). location.href is shorthand for window.location.href.They acting the same when you assign url to them - this will redirect to page which you assing, but you can see difference between them, when you open console (firebug, of developer tools) and write window.location and location.href

window.location.href = '@Url.Content("~/HTML/BrowserNotSupported.html")'; //Will take you to BrowserNotSupported.html.

window.open() example:

window.open('http://www.yahoo.com'); //This will open yahooin a new window.

Thursday, 26 June 2014

when should you use “with (nolock)” in SQL?

WITH (NOLOCK) is the equivalent of using READ UNCOMMITED as a transaction isolation level. So, you stand the risk of reading an uncommitted row that is subsequently rolled back, i.e. data that never made it into the database. So, while it can prevent reads being deadlocked by other operations, it comes with a risk. In a banking application with high transaction rates, it's probably not going to be the right solution to whatever problem you're trying to solve with it IMHO.

How to apply CSS on Hidden Button in HTML

CSS file:--
#btnPost[disabled="disabled"] {
    background: none;
}

html file:-
<button type='submit' class='leftArrow' id='btnPost' disabled='disabled'>Post</button>

This css file is used to remove the backgraound of the button.

How to create Widget in JQUERY..

(function ($) {

    $.widget('AZ.azAddNewDiscussion', $.AZ.Node, {
        options: {
          data: null      
        },
        widgetEventPrefix: $.AZ.Node.prototype.widgetEventPrefix,

        _create: function () {               // create event
            this._addNewDiscussion();

            this._on(this.element, {
                'click': function (event) {              
                },
                'click #newDiscussionNode': function (event) {
                    alert('button click');
                }

            });
        },
        _addNewDiscussion: function () {
     
          var buttonElemet = $("<h6 id='newDiscussionNode' style='text-align: right;color: #FFFFFF;margin-right: 1em;'>New Discussion</h6>");
          $(this.element).append(buttonElemet);

        }

    });

})(jQuery);