Wednesday, June 29, 2016

Abstract class VS Interface

abstract class vs interface c#

How to decide in which scenario Abstract class or Interface to be used?

-> When we are sure that design is not going to be changed then we should use interface. Otherwise we should use abstract class wherein we provide some functionality and anticipate multiple versions of your component

For e.g. When we know that there are going to be only 4 gears/Wheels to the care and not 5th one would be added then we choose to have an interface.
In case if there are changes to be done in interface then all the classes implementing it must be changed. Same is not with abstract class till the time we add non-abstract methods.

Static class

Static class

Below are some important point to consider for static classes.
  • Contains only static member (properties also)
  • Can not be instantiated
  • Static class is sealed
  • Can not contain Instance constructor (can contain static constructior)
* Instance constructor used to create & initialize any instance member when we use new to create object hence static class can not have  Instance constructor.

* Difference between sealed and static class is instance creation (sealed can contain static as well as non static)

Abstract class


Abstract class


  • Abstract classes are solely for inheritance
  • We can not create object of abstract class
  • Abstract can not be sealed or static
  • Abstract methods can not be private
  • Abstract methods can not have the body
  • Abstract method can not have the modifier virtual (By default it is virtual)
  • Access modifier of the abstract method should be same in both abstract and derived class
  • Abstract member can not be static
Overview in short
Private (No)
Static (No)
Sealed (No)
Virtual (No, by default virtual)



abstract class c# example



abstract class clsSave
{
    abstract public void Save();
}
class DerivedClass: clsSave
{
    // Area method is required to avoid
    // a compile-time error.
    public override void save()
    {
        //write code to save
    }
    static void Main()
    {
        DerivedClass objD= new DerivedClass();
        Console.WriteLine("Performing Save operation", objD.save());
    }
}

if condition in sql server 2008



if condition in sql

CREATE FUNCTION [dbo.[fnc_ValidateMilageReadings]
(
   @round_id INT,
   @depot_id INT,
)
RETURNS BIT

AS
BEGIN

DECLARE @openMilage NVARCHAR
DECLARE @firstStopMilage NVARCHAR
DECLARE @lastStopMilage NVARCHAR
DECLARE @closeMilage NVARCHAR
DECLARE @returnValue BIT

SET @returnValue = 0

SELECT @openMilage  = openMilage, @firstStopMilage = firstStopMilage, @lastStopMilage = lastStopMilage
FROM Transaction
WHERE roundId = @round_id AND depotId = @depot_id

IF (@closeMilage IS NULL) BEGIN SET @returnValue  = 0 END
ELSE IF (@closeMilage  = 0)  BEGIN SET @returnValue  = 0 END
ELSE IF @openMilage  < firstStopMilage BEGIN SET @returnValue  = 0 END
ELSE IF @firstStopMilage  < @lastStopMilage  BEGIN SET @returnValue  = 0 END
ELSE IF @lastStopMilage  < @closeMilage BEGIN SET @returnValue  = 0 END
ELSE @returnValue  = 1

RETURN @returnValue

END


Tuesday, June 28, 2016

Interfaces


  • We can only have internal access modifier for an interface (if we don't define also by default access modifier is internal for interface)
  • Interface can contain only signature of methods, properties, events or indexers

Decoupling can be Achieved through Interface



Lets say we have accounts class and invoice class. 
Now rather than communicating directly classes to classes (concrete classes), we can put a interface in between and both of these classes communicate via the interfaces

Example

interface IDBInterface
{
   void Update
}

class SQL : IDBInterface
{
  public void Update()
   {
     MessageBox.show("Updating SQL server.....");
   }
}

class Oracle : IDBInterface
 {
   Public void Update()
   {
     MessageBox.show("Updating oracle server...");
   }
}

class CommunicationChannel
{
  public void Update(IDBInterface idb)
  {
    idb.Update();
  }
}

=====================================================================
//Calling class
Class Myclass
{
  public void XYZ()
  {
    IDBInterface idb = new Oracle();
    CommunicationChannel cmc = new CommunicationChannel();
    cmc.Update(idb);
  }

Communication channel class is not directly communicating with concrete classes, it is communicating via interface. We can inject any class to communication channel class and the communication channel class will call the method.


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



Explicit Interface calling



interface Icontrol

{
  void paint();
}

interface Isurface
{
  void paint();

}

Way1 to call interface

class SampleClass : Icontrol,Isurface
{
  public void paint()
   {
      console.writeline("Hi");

   }
}

Explicit Calling (Way 2) 

class SampleClass : Icontrol,Isurface
{
   void Icontrol.paint()
    {
      console.writeline("IControl.paint");
     }
   void Isurface.paint()
    {
      console.writeline("Isurface.paint");

     }
}

Sunday, June 26, 2016

Four basic principles of OOP

Four major principles of Object-Oriented Programming (OOP)

  1. Data Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphysm


Data Abstraction

  • Any representation of data in which implementation details are hidden. (Showing only what is necessary)
  • Abstraction is a thought process (for e.g. we write design document with high level details such as method name and its return type i.e. we just say CalculateTax() without mentioning the rest of the details)



Encapsulation


  • Ability to provide users a well defined interface to a set of functions which hides internal working (Hide complexity)
  • Encapsulation is implementation side i.e. for e.g. while implementation all things are taken care such as IncomeAmount > 0 etc. which hides complexity while calling/using the method 


Inheritance

:
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.
 

Polymorphysm
: Polymorphysm is of two types
    Static and dynamic polymorphysm
  • Static
  • Dynamic

Saturday, June 25, 2016

Value and References types in C#


Value Type and References types in C#





Value Types

All numeric data types. For e.g. int x = 10
Boolean, character and Date
All structures even if their members are reference type
Enums


Reference Types

String (Internally the text is stored as a sequential read-only collection of character objects)
All arrays, even if their elements are value type
Class types such as Form
Delegates
*You must use New operator keyword when you initialize reference type.

Elements that are not Types
Namespaces
Modules
Events
Properties and Procedures
Variables, constants and fields.

In Depth
1. Value Type: A value type stores its contents in a Memory allocated on the stack. When you create a value type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both the variables work independently. 
*Value type can be created compile time and stored in stack memory, because of this Garbage Collector can’t access Stack
2. Reference Type: Reference Type are used by a reference which holds a reference to a object itself. Because reference types represents the address of the variable rather than data itself, assigning a reference variable to another variable doesn’t copy the data, insteadit creates a second copy of the reference, which refers to the same location of the heap as the original value (Can be marked for garbage collection)

Value type and reference type