Are there any improvements I can make to my implementation of the strategy pattern? I'm not sure about that if else statement in function Change().  I'm guessing there is a way to eliminate this and make better use of OOP principles.  Does anyone have any suggestions?  

PS This code is functional and produces the desired result.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1_testStrategy
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "mytext";

            Changer chng = new Changer();

            //THis is how I want the usage, I want the user to specify the string
            //and also which algorithm to invoke by specifyin the enumeration type
            string messageModified = chng.Change(message, types.type2); 
        }
    }

    //ENUMERATION OF TYPES
    public enum types
    {
        type1,
        type2
    }

    //Abstact strategy
    public interface IStringModifier
    {
        string Modify(string input);
    }

    //concrete strategy 1
    class type1 : IStringModifier
    {
        public string Modify(string input)
        {
            return "hello1";
        }
    }

    //conctrete strategy 2
    class type2 : IStringModifier
    {
        public string Modify(string input)
        {
            return "hello2";
        }
    }


    //MAIN API I WANT USER TO ACCESS
    public class Changer
    {
        public Changer()
        {
        }

        private IStringModifier _stringModifier;

        public void SetStringModifier(IStringModifier stringModefier){

            this._stringModifier = stringModefier;
        }

        public string Change(string text, types type) //not sure about this part
        {
            if (type == types.type1)
            {
                SetStringModifier(new type1());
            }
            else if (type == types.type2)
            {
                SetStringModifier(new type2());
            }

            return _stringModifier.Modify(text);
        }

    }
}
You can do this more elegantly by specifying a Func delegate instead of an enum and a set of interfaces.

class Program
{
    static void Main(string[] args)
    {
        string message = "mytext";

        Changer chng = new Changer();

        string messageModified = chng.Change(message, Strategy2); 
    }

    // These methods have a Func&lt;string,string&gt; signature
    // i.e. accept string as parameter and return a string
    static string Strategy1(string input)
    {
        return "hello1";
    }
    static string Strategy2(string input)
    {
        return "hello2";
    }
}

public class Changer
{
    public Changer()
    {
    }

    private Func&lt;string,string&gt; _stringModifier;

    public void SetStringModifier(Func&lt;string,string&gt; stringModifier)
    {
        this._stringModifier = stringModifier;
    }

    public string Change(string text, Func&lt;string,string&gt; stringModifier)
    {
        SetStringModifier(stringModifier); // why?

        return _stringModifier(text);
    }
}



  what was meant by the comment //why?


I would have thought that one of the following might be better.

Either, set the strategy once and use it repeatedly, like this ...

public class Changer
{
    public Changer()
    {
    }

    private Func&lt;string,string&gt; _stringModifier;

    public void SetStringModifier(Func&lt;string,string&gt; stringModifier)
    {
        this._stringModifier = stringModifier;
    }

    public string Change(string text)
    {
        return _stringModifier(text);
    }
}


... or like this ...

public class Changer
{
    public Changer(Func&lt;string,string&gt; stringModifier)
    {
        this._stringModifier = stringModifier;
    }

    private Func&lt;string,string&gt; _stringModifier;

    public string Change(string text)
    {
        return _stringModifier(text);
    }
}


Or, pass the strategy into the Change method every time, in which case there's no need to store it in as member data, like this ...

public class Changer
{
    public Changer()
    {
    }

    public string Change(string text, Func&lt;string,string&gt; stringModifier)
    {
        return stringModifier(text);
    }
}



  Thank you, another question is, wouldn't this allow the user to just write their own strategy and pass it to chng.Change() method?


Yes it would. That's often a good thing: it makes the class more 'flexible', so that it can be used in ways you hadn't imagined.


  And if my end goal is to make this an API, is that acceptable? Should I restrict them like I did in my original post using the enumeration?


I don't know. If you want to keep the enum, you could still use a Func delegate instead of an interface with several subclasses (and use a switch statement instead of repeated if/else).

public class Changer
{
    public Changer()
    {
    }

    private Func&lt;string,string&gt; _stringModifier;

    public void SetStringModifier(Types type)
    {
        switch (type)
        {
            case Types.Type1:
                this._stringModifier = Strategy1;
                break;
            case Types.Type2:
                this._stringModifier = Strategy2;
                break;
            default:
                throw new NotImplementedException();
    }

    public string Change(string text)
    {
        return _stringModifier.Modify(text);
    }

    static string Strategy1(string input)
    {
        return "hello1";
    }
    static string Strategy2(string input)
    {
        return "hello2";
    }
}