Sunday, September 2, 2018

What is Extension Method in c#

Extension Method:-

Extension Method is a Static method of Static class where the "this" modifier is applied to the first
parameter . The type of first parameter will be the type of Extended method.


Benefits of Extended method:

1. Extension method allow existing class to be extended without relying on inheritance.
  or having change the class source code.

2. If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.




What is Collection?

1. What is Collection-

Ans: Collection is group of object that holds many value in specific series is called collection.

Two Types of Collection:

1. Generic Collection

2. Non- Generic Collection.


1. Non-Generic Collection: 

1. Array List  2.Has table 3. sorted list 4. Stack 5. Queue 

Generic List:

1. List 2. Dictionary 3. Stack 4. Queue

1. What is Array List:

Ans: Array list whose size are dynamically increased.
Namespace: System.collection

1.We can store same type of data in array but  Array list based on the object so we can store any type of data whether it is int, string, double or any custom data type.

Hash table: 

It is collection of Key value and pair.

Namespace: System.Collection. It is Similar to generic Dictionary.


Difference b/w Hash table and Dictionary.

Hash table and Dictionary are collection of  data structure hold data in the form of key value and pair.

1. Hash table is not generic type. while dictionary is generic type.

2. Hash table is Thread safe while Dictionary is not.

3. Dictionary is type safe means value need not  be boxing. while hash table need boxing and Un boxing.

4.In Dictionary,  when you try to access the value of key which is not exist in the collection. it       through error Key not find. while hash table returns null value.

5. when using large collection of key value pair hash table is more Efficient then dictionary.

6. When we retrieve the record in collection the hashtable does not maintain the order of entries while dictionary maintains the order of entries by which entries were added.


Difference between Array and Array List

1. Array is a strongly type it contains only specific type of items/element.
but Arraylist can store any types of items.

2. In array we can store only one type of datatypes either int,string,float.
but array list can store all types of data.

3. array belongs to the system.array namespace. while array list belongs to the system.collection
namespace

4.array size was fixed but array list is dynamic size.










Collection:

* Collection is group of object that can help with store, manipulate the date more flexible way.

Two types of collection
1. Generic 2. non-generic

1. Generic

List
Stack
Queue
LinkedList
HashSet
SortedSet
Dictionary
SortedDictionary
SortedList

2. Non-Generic

ArrayList
Stack
Queue
Hashtable

===================================

List:

C# List<T> class is used to store and fetch elements. It can have duplicate elements.
It is found in System.Collections.
......................................................................................


HashSet:

 C# HashSet class can be used to store, remove or view elements.
 It does not store duplicate elements.
 It is suggested to use HashSet class if you have to store only unique elements.
 It is found in System.Collections.Generic namespace.


 SortedSet:

 C# SortedSet class can be used to store, remove or view elements.
 It maintains ascending order and does not store duplicate elements.
 It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order.
 It is found in System.Collections.Generic namespace.


 =======================================================================================================
Stack<T>

C# Stack<T> class is used to push and pop elements.
It uses the concept of Stack that arranges elements in LIFO (Last In First Out) order.
It can have duplicate elements. It is found in System.Collections.Generic namespace

Example:

public static void Main(string[] args) 
    { 
        Stack<string> names = new Stack<string>(); 
        names.Push("Sonoo"); 
        names.Push("Peter"); 
        names.Push("James"); 
        names.Push("Ratan"); 
        names.Push("Irfan"); 

    }

=============================================================================
C# Queue<T>

C# Queue<T> class is used to Enqueue and Dequeue elements.
It uses the concept of Queue that arranges elements in FIFO (First In First Out) order.
It can have duplicate elements. It is found in System.Collections.Generic namespace.

====================================================================================


C# Dictionary<TKey, TValue>

C# Dictionary<TKey, TValue> class uses the concept of hashtable.
It stores values on the basis of key. It contains unique keys only. By the help of key, we can easily search or remove elements.
It is found in System.Collections.Generic namespace.

====================================================================================================
































































Sunday, August 12, 2018

In what scenarios will you use a abstract class and in what scenarios will you use a interface?

Ans:

If you want to increase re usability in inheritance then abstract classes are good. If you want implement or force some methods across classes must be for uniformity you can use a interface. So to increase  re usability via inheritance use abstract class as it is nothing but a base class and to force methods use interfaces.


Abstract Class:

When we have the requirement of a class that contains some common properties or methods with some common properties whose implementation is different for different classes, in that situation, it's better to use Abstract Class then Interface.




Example:

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace oops1  
  6. {  
  7.     public abstract  class Cars  
  8.     {  
  9.   
  10.         //put all the common functions but diffrent implementation in  abstract method.  
  11.         public abstract double price();  
  12.         public abstract int getTotalSeat();  
  13.         public abstract string colors();  
  14.   
  15.         //put all the common property in normal class  
  16.         public string Wheel()  
  17.         {  
  18.             return "4 wheeler";  
  19.   
  20.         }  
  21.         public string CheckAC()  
  22.         {  
  23.             return "AC is available";  
  24.         }  
  25.         public string CallFacility()  
  26.         {  
  27.             return "Call Facility  supported";  
  28.         } 
  29.    }
  30.  }


Important:


So, in those cases, we will use abstract class where we want to restrict the user from creating the object of parent class because by creating object of parent class, you can't call child class methods. So, the developer has to restrict accidental creation of parent class object by defining it as abstract class.

So, I think, in these ways, we can use abstract class in our real time project.




Interface:





Now, I will narrate a real time scenario where we can use interface. I will go for the same example that I have done earlier.
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace oops1  
  6. {  
  7.     public   class Cars  
  8.     {  
  9.   
  10.         public string Wheel()  
  11.         {  
  12.             return "4 wheeler";  
  13.   
  14.         }  
  15.         public string CheckAC()  
  16.         {  
  17.             return "AC is available";  
  18.         }  
  19.         public string CallFacility()  
  20.         {  
  21.             return "Call Facility  supported";  
  22.         }  
  23.          
  24.         
  25.   
  26.   
  27.   
  28.     }  
  29.   
  30.      
  31. }  
Now, here is my Toyota class which is derived from Cars.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace oops1  
  8. {  
  9.     public class Toyota : Cars  
  10.     {  
  11.           
  12.   
  13.         static void Main(string[] args)  
  14.         {  
  15.   
  16.   
  17.             Toyota Toy = new Toyota();  
  18.               
  19.              
  20.   
  21.             Console.WriteLine(Toy.CallFacility());  
  22.             Console.WriteLine(Toy.Wheel());  
  23.             Console.WriteLine(Toy.CheckAC());  
  24.              
  25.              
  26.             
  27.             Console.ReadLine();  
  28.   
  29.   
  30.         }  
  31.     }  
  32.   
  33.   
  34. }  
Here is my Hyundai class which is derived from Cars.
  1. using oops1;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace oops1  
  9. {  
  10.     public class Hyundai:Cars   
  11.     {  
  12.          
  13.   
  14.         static void Main(string[] args)  
  15.         {  
  16.             Hyundai dust = new Hyundai();  
  17.   
  18.             Console.WriteLine(dust.CallFacility());  
  19.             Console.WriteLine(dust.Wheel());  
  20.             Console.WriteLine(dust.CheckAC());  
  21.              
  22.   
  23.             Console.ReadLine();  
  24.   
  25.   
  26.         }  
  27.     }  
  28.   
  29.      
  30. }  
A new feature for the Hyundai car is Introduced called GPS which is not supported in Toyota cars. Then, how can we implement and which way we can implement these?

Here, we have certain options like
  • Go for a new class defining the GPS method and inherit it to the Hyundai Class.
  • Go for an abstract class and define GPS method and inherit it on Hyundai class and implement the GPS method there.
  • Directly create a method in Hyundai class and consume it.
  • Go for Interface
    .
    CASE 1 - By Using simple class
    Let's find what will happen if we use a class there, and declare a method as GPS and try to inherit in Hyundai class.
    Created a new class as "NewFeatures, as shown below.
    1. class NewFeatures 
    2.    {  
    3.        public void GPS()  
    4.        {  
    5.            Console.WriteLine("GPS supported");  
    6.        }  
    7.    }  
    Now, inherit the Hyundai class from NewFeatures. So, if we check this class, it was previously inherited from Cars class and for now, again inherits from NefFeatures class. So, it gives the following error when you try to run the program.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,NewFeatures  
    11.     {  
    12.          
    13.   
    14.         static void Main(string[] args)  
    15.         {  
    16.             Hyundai hun = new Hyundai();  
    17.   
    18.             Console.WriteLine(hun.CallFacility());  
    19.             Console.WriteLine(hun.Wheel());  
    20.             Console.WriteLine(hun.CheckAC());  
    21.              
    22.   
    23.             Console.ReadLine();  
    24.   
    25.   
    26.         }  
    27.     }  
    28.   
    29.      
    30. }  
    Now, run the program and find out the error.



    This is simple because C# does not support multiple inheritance.
  • CASE 2 - By using Abstract class

    Now, go for abstract class and see what happens.
    1. public abstract class  NewFeatures  
    2.    {  
    3.       abstract public void GPS();  
    4.          
    5.    }  
    Now, let's try to inherit from abstract class.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,NewFeatures  
    11.     {  
    12.        public  override  void GPS()  
    13.         {  
    14.             Console.WriteLine("GPS supported.");  
    15.   
    16.         }  
    17.   
    18.   
    19.         static void Main(string[] args)  
    20.         {  
    21.             Hyundai hun = new Hyundai();  
    22.   
    23.             Console.WriteLine(hun.CallFacility());  
    24.             Console.WriteLine(hun.Wheel());  
    25.             Console.WriteLine(hun.CheckAC());  
    26.              
    27.   
    28.             Console.ReadLine();  
    29.   
    30.   
    31.         }  
    32.     }  
    33.   
    34.      
    35. }  
    So, here is the error I got.

  • CASE 3 - Direct creating a method called GPS() inside Hyundai class
    This is very relevant way to use a unique method but it has a small problem. If for any reason we forget to create such common method, then it will not ask to write methods.Today, we are using one unique method so that is OK we can remember and write the method. Suppose, we have hundreds of such common methods and we forget to  write 2 of them then it will run but not give the expected output,so we have to skip the way and go for the fourth one by defining interface.
  • CASE 4 - By defining Interface
    This is the most important case and we will see how it will solve our problem.Lets define  the  method in an Interface
    1. interface  INewFeatures  
    2.  {  
    3.     void GPS();  
    4.        
    5.  }  
    Now inherit the Interface from Cars and see without implementing the GPS() method.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars, INewFeatures  
    11.     {  
    12.   
    13.   
    14.         static void Main(string[] args)  
    15.         {  
    16.             Hyundai hun = new Hyundai();  
    17.   
    18.             Console.WriteLine(hun.CallFacility());  
    19.             Console.WriteLine(hun.Wheel());  
    20.             Console.WriteLine(hun.CheckAC());  
    21.              
    22.   
    23.             Console.ReadLine();  
    24.   
    25.   
    26.         }  
    27.     }  
    28.   
    29.      
    30. }  
    As we have not implemented the method it will show the following error as follow.

    So, the problems which happen in case 3 can be easily solved by using Interface. Now, let's implement the method and see will it solve the problem which arises in Case1 and case2 as follows.
    1. using oops1;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7.   
    8. namespace oops1  
    9. {  
    10.     public class Hyundai:Cars,INewFeatures  
    11.     {  
    12.         public  void GPS()  
    13.         {  
    14.             Console.WriteLine("GPS supported.");  
    15.   
    16.         }  
    17.   
    18.   
    19.         static void Main(string[] args)  
    20.         {  
    21.             Hyundai hun = new Hyundai();  
    22.   
    23.             Console.WriteLine(hun.CallFacility());  
    24.             Console.WriteLine(hun.Wheel());  
    25.             Console.WriteLine(hun.CheckAC());  
    26.             hun.GPS();  
    27.              
    28.   
    29.             Console.ReadLine();  
    30.   
    31.   
    32.         }  
    33.     }  
    34.   
    35.      
    36. }  


    Thus, it resolves the demerits of all the above 3 cases. So, in this situation, we have to use Interface. Here, I have shown you the sketch diagram.
In this way, we can u