Thursday, December 3, 2015

C# - Types of Polymorphism in C#.Net | Basic Polymorphism in C#.NET

Polymorphism

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means“multiple” and morph means “forms” so polymorphism means many forms.

In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

In Polymorphism we have 2 different types those are:

1) Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)
2) Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

Compile Time Polymorphism

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

Example

public class Class1
{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}

In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding.

Run Time Polymorphism

Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual& override” keywords.

In base class if we declare methods with virtualkeyword then only we can override those methods in derived class using override keyword

Example

//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}

Tuesday, December 1, 2015

Singleton Design pattern !!!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace PCardAutomation

{
public class PCardLogger

{
public static PCardLogger Instance

{
get

{
if (_Instance == null)

{
lock (_SyncRoot)
{
if (_Instance == null)

_Instance = new PCardLogger();
                       
}
}
return _Instance;


}

}
private static volatile PCardLogger _Instance;

private static object _SyncRoot = new Object();

private PCardLogger()

{

}
public StreamWriter Writer { get; set; }

public StreamWriter WriterGlobalLog { get; set; }

public string LogPath { get; set; }

public string LogFileName { get; set; }

public string LogFileExtension { get; set; }

public string LogFile { get { return LogFileName + LogFileExtension; } }

public string LogFullPath { get { return Path.Combine(LogPath, LogFile); } }

public bool LogExists { get { return File.Exists(LogFullPath); } }

public void WriteLineToLog(String inLogMessage)

{
WriteToLog(inLogMessage + Environment.NewLine);


}
public void WriteToLog(String inLogMessage)


{
if (!Directory.Exists(LogPath))

{
Directory.CreateDirectory(LogPath);

}
using (Writer = new StreamWriter(LogFullPath, true))

{

Writer.Write(inLogMessage);

}

}
public static void WriteLine(String inLogMessage)

{

Instance.WriteLineToLog(inLogMessage);

}
public static void Write(String inLogMessage)

{

Instance.WriteToLog(inLogMessage);

}

}

}

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

WRITING(CALLING THE CLASS) THE LOG:

PCardAutomation.PCardLogger.Instance.LogFileName = "Global_PCardLog_" + DateTime.Now.ToString("yyyyMMdd"); ;

PCardAutomation.PCardLogger.Write(DateTime.Now + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("ANNUAL CARD :" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("" + Environment.NewLine);

PCardAutomation.PCardLogger.Write(ex.ToString() + Environment.NewLine);

PCardAutomation.PCardLogger.Write("" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("******************************" + Environment.NewLine);

PCardAutomation.PCardLogger.Write("" + Environment.NewLine);

 

How to: Explicitly Implement Members of Two Interfaces in C#

METHOD ---I

( If the two interface members perform the same function).

(If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. In the following example, all the calls to Paint invoke the same method).


class Test
{
 static void Main()
  {
       SampleClass sc = new SampleClass();
       IControl ctrl = (IControl)sc;
       ISurface srfc = (ISurface)sc;

       // The following lines all call the same method.

       sc.Paint();
       ctrl.Paint();
       srfc.Paint();
   }
}


interface IControl
{
 void Paint();
}

 interface ISurface
{
void Paint();
 }

class SampleClass : IControl, ISurface
{
 // Both ISurface.Paint and IControl.Paint call this method.

public void Paint()
  {
    Console.WriteLine("Paint method in SampleClass");
   }
}

// Output:
// Paint method in SampleClass
// Paint method in SampleClass
// Paint method in SampleClass


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

METHOD --II

( If the two interface members do not perform the same function).


public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}




// Call the Paint methods from Main.

SampleClass obj = new SampleClass();
//obj.Paint();  // Compiler error.

IControl c = (IControl)obj;
c.Paint();  // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass.
// Output:
// IControl.Paint
// ISurface.Paint