Saturday, December 22, 2018

What is Manage and Unmanage Code

Manage Code:

Managed code is a code which is executed by CLR (Common Language Runtime) i.e all application code based on .Net Platform. It is considered as managed because of the .Net framework which internally uses the garbage collector to clear up the unused memory.

Unmanaged Code:

Unmanaged code is any code that is executed by application runtime of any other framework apart from .Net. The application runtime will take care of memory, security and other performance operations.


Sunday, December 16, 2018

Difference between Finalize and Dispose?

Ans:

The main difference between dispose() and finalize() is that the method dispose() has to be explicitly invoked by the user whereas, the method finalize() is invoked by the garbage collector, just before the object is destroyed.


BASIS FOR COMPARISONDISPOSE( )FINALIZE( )
DefinedThe method dispose( ) is defined in the interface IDisposable interface.The method finalize( ) id defined in java.lang.object class.
Syntaxpublic void Dispose( ){
// Dispose code here
}
protected void finalize( ){
// finalization code here
}
InvokedThe method dispose( ) is invoked by the user.The method finalize( ) is invoked by the garbage collector.
PurposeMethod dispose( ) is used to free unmanaged resources whenever it is invoked.Method finalize( ) is used to free unmanaged resources before the object is destroyed.
ImplementationThe method dispose( ) is to be implemented whenever there is a close( ) method.The method finalize( ) is to be implemented for unmanaged resources.
Access specifierThe method dispose( ) is declared as public.The method finalize( ) is declared as private.
ActionThe method dispose( ) is faster and instantly disposes an object.The method finalize is slower as compared to dispose
PerformanceThe method disposes( ) performs the instantaneous action hence, does not effect the performance of websites.The method finalize( ) being slower affects the performance of the websites.

What is Using & Why we use Using?

Ans:

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.


The using statement tells .NET to release the object specified in the using block once it is no longer needed.


using (B a = new B())
{
   DoSomethingWith(a);
}
is equivalent to
B a = new B();
try
{
  DoSomethingWith(a);
}
finally
{
   ((IDisposable)a).Dispose();
}
if we declare class object in under using state it got compellation error convert to Idispoable.

Tuesday, December 4, 2018

What is a nested class? And when should you use nested classes in your C# programs?

The class B here is enclosed inside the declaration of class A. Class B is thus a nested class. Because it has a public accessibility modifier, it can be accessed in places other than class A's scope.

We create an instance of A and an instance of A.B. The instance of A does not contain an instance of B.

Example:

class A
{
    public int _v1;

    public class B============nested classs
    {
        public int _v2;
    }
}

class Program
{
    static void Main()
    {
        A a = new A();
        a._v1++;

        A.B ab = new A.B();
        ab._v2++;
    }
}

Note: The main feature of nested classes is that they can access private members of the outer class while having the full power of a class itself. Also they can be private which allows for some pretty powerful encapsulation in certain circumstances:\

  • A nested class can be declared as a private, public, protected, internal, protected internal, or private protected.
  • Outer class is not allowed to access inner class members directly as shown in above example.
  • You are allowed to create objects of inner class in outer class.
  • Inner class can access static member declared in outer class as shown in the below example:
  • Inner class can access non-static member declared in outer class as shown in the below example:

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