Tuesday, 23 April 2019

Difference between == and Equals() in c#

= = operator  compares if object references are same
.Equals() method compares if the contents are same not the object references
In case of string, it always compares on contents not the object references

Example: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //case-1
            string name = "Ashish";
            string myName = name;

            Console.WriteLine("== operator result is {0}", name == myName); // True
            Console.WriteLine("Equals method result is {0}", name.Equals(myName)); // True

            //case-2
            object name1 = "sandeep";
            char[] values = { 's', 'a', 'n', 'd', 'e', 'e', 'p' };
            object myName1 = new string(values);
            Console.WriteLine("== operator result is {0}", name1 == myName1); //False
            Console.WriteLine("Equals method result is {0}", myName1.Equals(name1)); // True

            //case-3
            string name2 = "sandeep";
            char[] values2 = { 's', 'a', 'n', 'd', 'e', 'e', 'p' };
            string myName2 = new string(values2);
            Console.WriteLine("== operator result is {0}", name2 == myName2); //True
            Console.WriteLine("Equals method result is {0}", myName2.Equals(name2)); // True

            //case-3
            string name3 = "sandeep";
            string myName3 = null;
            Console.WriteLine("== operator result is {0}", name3 == myName3); //False
            Console.WriteLine("Equals method result is {0}", myName3.Equals(name3)); // Throw error


            Console.ReadKey();
        }
    }
}

Monday, 22 April 2019

What is grep function in jquery

jQuery $.grep() Method. The jQuery method of $.grep() is used to filter the contents of an array

Example:- 

<html>
<head>
  <title>jQuery grep() function</title>
  <style>
  div {
    color: blue;
  }
  p {
    color: red;
    margin: 0;
  }

  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div></div>
<p></p>

<script>
var arr1 = [ 1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2 ];
$( "div" ).text( arr1.join( ", " ) );

arr1 = jQuery.grep(arr1, function( n, i ) {
  return ( n !== 5 && i > 6 );
});


$( "p" ).text( arr1.join( ", " ) );
</script>

</body>

</html>


Output: -

1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2
3, 7, 3, 8, 8, 2