Sunday, 11 February 2018

Difference between == and .Equals method in c#

For Value Type:
== and .Equals() method usually compare two objects by value.

For Example:

int x = 20;

int y = 20;

Console.WriteLine( x == y);

Console.WriteLine(x.Equals(y));

Output:

True

True

For Reference Type:

== performs an identity comparison,
i.e. it will only return true
if both references point to the same object.
While Equals() method is expected to
perform a value comparison, i.e. it will
return true if the references point
to objects that are equivalent.

For Example:
StringBuilder s1 = new StringBuilder(“Yes”);

StringBuilder s2 = new StringBuilder(“Yes”);

Console.WriteLine(s1 == s2);

Console.WriteLine(s1.Equals(s2));

Output:

False

True

In above example, s1 and s2 are different objects hence “==” returns
false, but they are equivalent hence “Equals()”
method returns true. Remember there is an exception of this rule,
i.e. when you use “==” operator with string class it
compares value rather than identity.

When to use “==” operator and when to use “.Equals()” method?

For value comparison, with Value Type use “==” operator
and use “Equals()” method while performing value comparison
with Reference Type.

Return Multiple values from a function in C#

In C#, There are 4 ways to return multiple values from a C# function.
  • Using KeyValue pair
  • Using ref/out parameters
  • Using Struct or Class
  • Using Tuple
Method_Returns_Multiple_Values

1. Using KeyValue pair:


class Program
    {
        static void Main(string[] args)
        {
            int int1 = 15;
            int int2 = 25;
            var result = Add_Multiply(int1, int2);
            Console.WriteLine(result.Key);
            Console.WriteLine(result.Value);
        }
        private static KeyValuePair<int, int> Add_Multiply(int int1, int int2)
        {
            var KeyValuePair = new KeyValuePair<int, int>(int1 + int2, int1 * int2);
            return KeyValuePair;
        }
    }

Output:

40
375

2.a. Using Ref Parameter:


 class Program
    {
        static void Main(string[] args)
        {
            int int1 = 15;
            int int2 = 25;
            int add = 0;
            int multiply = 0;
            Add_Multiply(int1, int2, ref add, ref multiply);
            Console.WriteLine(add);
            Console.WriteLine(multiply);
        }
        private static void Add_Multiply(int int1, int int2, ref int add, ref int multiply)
        {
            add = int1 + int2;
            multiply = int1 * int2;
        }
    }

40
375

2.b. Using Out Parameter:

class Program
    {
        static void Main(string[] args)
        {
            int int1 = 15;
            int int2 = 25;
            int add = 0;
            int multiply = 0;
            Add_Multiply(int1, int2, out add, out multiply);
            Console.WriteLine(add);
            Console.WriteLine(multiply);
        }
        private static void Add_Multiply(int int1, int int2, out int add, out int multiply)
        {
            add = int1 + int2;
            multiply = int1 * int2;
        }
    }

Output:

40
375

3.a. Using Struct:

class Program
    {
        struct Result
        {
            public int add;
            public int multiply;
        }
        static void Main(string[] args)
        {
            int int1 = 53;
            int int2 = 17;
            var result = Add_Multiply(int1, int2);
            Console.WriteLine(result.add);
            Console.WriteLine(result.multiply);
        }
        private static Result Add_Multiply(int int1, int int2)
        {
            var result = new Result
            {
                add = int1 + int2,
                multiply = int1 * int2
            };
            return result;
        }
    }

Output:

70
901

3.b. Using Class:


struct Result
        {
            public int add;
            public int multiply;
        }
        static void Main(string[] args)
        {
            int int1 = 13;
            int int2 = 27;
            var result = Add_Multiply(int1, int2);
            Console.WriteLine(result.add);
            Console.WriteLine(result.multiply);
        }
        private static Result Add_Multiply(int int1, int int2)
        {
            var result = new Result
            {
                add = int1 + int2,
                multiply = int1 * int2
            };
            return result;
        }
    }

Output:

40
351

4. Using Tuple:


class Program
{
        static void Main(string[] args)
        {
            int int1 = 25;
            int int2 = 28;
            var result = Add_Multiply(int1, int2);
            Console.WriteLine(result.Item1);
            Console.WriteLine(result.Item2);
        }
        private static Tuple<int, int> Add_Multiply(int int1, int int2)
        {
            var tuple = new Tuple<int, int>(int1 + int2, int1 * int2);
            return tuple;
        }

}

Remove Duplicate characters from String in C#

class Program
    {
        static void Main()
        {
            string value1 = RemoveDuplicateChars("Csharpstar");
            string value2 = RemoveDuplicateChars("Google");
            string value3 = RemoveDuplicateChars("Yahoo");
            string value4 = RemoveDuplicateChars("CNN");
            string value5 = RemoveDuplicateChars("Line1\nLine2\nLine3");
 
            Console.WriteLine(value1);
            Console.WriteLine(value2);
            Console.WriteLine(value3);
            Console.WriteLine(value4);
            Console.WriteLine(value5);
        }
 
        static string RemoveDuplicateChars(string key)
        {
            // --- Removes duplicate chars using string concats. ---
            // Store encountered letters in this string.
            string table = "";
 
            // Store the result in this string.
            string result = "";
 
            // Loop over each character.
            foreach (char value in key)
            {
                // See if character is in the table.
                if (table.IndexOf(value) == -1)
                {
                    // Append to the table and the result.
                    table += value;
                    result += value;
                }
            }
            return result;
        }
    }

Input: Google
Output: Gogle

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...