#5 Ref and Out Parameters


int NormalVaribale = 1;
 int referencevariable = 1;
 int outvaraibale = 1;

Normalmethod(NormalVaribale);
 referencemethod(ref referencevariable);
 outmethod(out outvaraibale);
 Console.WriteLine("Normal Variable"+NormalVaribale);
 Console.WriteLine("(REF)Reference Variable"+referencevariable);
 Console.WriteLine("(out) Out Varaible"+outvaraibale);

ref :Value must be Initialized before it is passed to the method. Reference is being passed to the method,so the value changed will get reflected./*It has got Nothing to do with value type and reference type both of these can use ref*/

out:Value need not be Assigned,but has to be defined before the method exists.

Note:Even though you have initialized before passing to method,you got to assign value before the method exists.

 

#4 Static Constructors

1.Static Constructors cannot have Access Modifiers  or have parameters.

2.They are initialized before  any static members are referenced.They are the First One to get Initialized.

3.A non static class can have static constructor.

class Test
{ 
static Test() //cannont have access modifier
{
Console.WriteLine("Hello World");
}
}

 

 

 

 

#3 Decorator Pattern

When to Use Decorator Pattern?

Add New Responsibility(functionality) to the class,without breaking the existing functionality.

Usage:

1.Component as abstract class/Interface.

2.Concrete Components Implementing the Components.

3.Decorator as abstract class/Interface with Component as member initialized via constructor.

4.Concrete Decorator Implements Decorator.

Consequence:
Functionality are added at runtime,Hard to debug(Since object implements both Concrete Component and Concrete Decorator).

 Source Code:https://github.com/rangesh-/Design-Patterns/tree/master/Design%20Patterns/Decorartor%20Pattern

#2:Enum With Flags

Enum can take Flags as attribute and perform bit operations over the numerics(Values must be power of two).

[Flags]
public enum Color
{
Red=1,-->0001
Yellow=2,--->0010
Green=4--->0100
}

var combination = Color.Red | Color.Green;
Console.WriteLine(combination); /* Red,Green*/

They are helpful when you need to hold multiple values/Combination  of values defined in an Enum.

State Pattern

When to Use State Pattern?

Lets Assume a Case where we design a CardReader System.The specification of  the CardReader would be something like at a given point of time the machine can either insert or discard or hold the card for processing.so we find the Usage of State in CardReader .

State of CardReader:

Hold,Discard,Insert

Normal Approach:

A Normal Approach would be to have an Enum with specific state and traditional switch case performing various task of the CardReader.

Code:

public enum CardReaderState
{
Insert,
Discard,
Hold

}
class CardReader
{
private CardReaderState state=CardReaderState.Insert;
public void PerformCardOperation()
{
switch (state)
{
case CardReaderState.Discard:
IdleState();

break;
case CardReaderState.Insert:
state = CardReaderState.Hold;
InsertCard();

break;
case CardReaderState.Hold:
state = CardReaderState.Discard;
CardTasks();
break;
}

}

private void CardTasks()
{
Console.WriteLine(“CardReader Holding the Card For Processing”);
PerformCardOperation();
}

private void IdleState()
{
Console.WriteLine(“CardReader is Idle”);
}

private void InsertCard()
{
Console.WriteLine(“Card Inserted”);
PerformCardOperation();

}
}

Problem with the Above Code:It Solves the Problem,but it clearly fails against Open/Closed Principles and lacks oo principles.what if we need another state introduced,we must modify the enum and alter the switch cases.An addition might introduce new bug and break the functionality.

Solution:

The State Pattern Comes to the rescue.

Definition:Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

My Definition:

1.Create a abstract Class which every state must perform.

2.Override the State behavior with corresponding states.

3.Chain the Next State in the Respective State.

Step 1:

1,Abstract Class :

public abstract class CardState
{
protected CardReader _reader; 

public virtual void PerformCardOperation()
{

}
}

2.State Class for every states

public class CardInsert : CardState
{

public CardInsert(CardReader Reader)
{
_reader = Reader;
}

public override void PerformCardOperation()
{
Console.WriteLine(“Card Inserted”);
_reader.CurrenState=new CardHold(_reader);//Chaining the next States
}

}

public class CardHold : CardState

{
public CardHold(CardReader reader)
{
_reader = reader;
}
public override void PerformCardOperation()
{
Console.WriteLine(“Card Holded For Performing Operation”);
_reader.CurrenState=new CardDiscard(_reader);//Chaining the next state

}

}

public class CardDiscard : CardState
{
public CardDiscard(CardReader reader)
{
_reader = reader;
}
public override void PerformCardOperation()
{
Console.WriteLine(“Card discarded”);
}
}

3.Chain the Respective State : 

In the above code this is the line

_reader.CurrenState=new CardHold(_reader);

which does the chaining the next state.i.e from insert to hold and then to Discard.