soarcreator
soarcreator

Reputation: 21

How to implement functions like template in C#

How can I implement functions like template in C#? For example, FuncA, FuncB, and FuncC functions take the same arguments, and then those functions always just do the same operation with ClassA. Even if those function take ClassB or three integers, those function convert ClassB or three integers into ClassA to do the same operation as when taking the ClassA as an argument. I am not sure if there is a good way, but if you know, please let me know.

void FuncA(ClassA a)
{
    // do something
}
void FuncA(ClassB b)
{
    FuncA(b.ToA());
}
void FuncA(int x, int y, int z)
{
    FuncA(new ClassA(x, y, z));
}

void FuncB(ClassA a)
{
    // do something
}
void FuncB(ClassB b)
{
    FuncB(b.ToA());
}
void FuncB(int x, int y, int z)
{
    FuncB(new ClassA(x, y, z));
}

void FuncC(ClassA a)
{
    // do something
}
void FuncC(ClassB b)
{
    FuncC(b.ToA());
}
void FuncC(int x, int y, int z)
{
    FuncC(new ClassA(x, y, z));
}

// FuncD, FuncE and so on...

Upvotes: 0

Views: 71

Answers (3)

soarcreator
soarcreator

Reputation: 21

I solved this problem by using implicit conversion and tuple.

class ClassA
{
    // members...

    public static implicit operator ClassA(ClassB b)
    {
        return b.ToA();
    }

    public static implicit operator ClassA(Tuple<int, int, int> t)
    {
        return new ClassA(t.Item1, t.Item2, t.Item3);
    }
}

void FuncA(ClassA a)
{
    // do something
}
// we don't have to implement overloaded FuncA functions that take ClassB or three integers.
void FuncB(ClassA a) { /* do something */ }
void FuncC(ClassA a) { /* do something */ }

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81493

This may not be what you are looking for, however it might give you something to think about

public class MyBase
{
   public int X { get; set; }
   public int Y { get; set; }
   public int Z { get; set; }
   public void MethodA()
   {

   }
   public void MethodB()
   {

   }
   public void MethodC()
   {

   }
}

public class ClassA : MyBase
{
   public ClassA()
   {

   }
   public ClassA(int x, int y, int z)
   {
      X = x;
      Y = y;
      Z = z;
   }
}
public class ClassB : MyBase
{

}

Usage

var classA = new ClassA(23,56,324);
classA.MethodA();
classA.MethodB();
classA.MethodC();

var classB = new ClassB();
classB.MethodA();
classB.MethodB();
classB.MethodC();

Upvotes: 0

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

I imagine a couple options for this...

You could create a base class:

public abstract class ActionerOnA
{
    public abstract void Execute(A a);

    public void Execute(B b) => Execute(b.ToA());
    public void Execute(int x, int y, int z) => Execute(new A(x, y, z));
}

public class Action1 : ActionerOnA
{
    public override void Execute(A a)
    {
        DoSomethingHereWith(a);
    }

    // no need to worry about the other overloads
}

You would use this with:

var actionerOnA = new Action1();
actionerOnA(a);
actionerOnA(b);
actionerOnA(x, y, z);

You could use a helper class, where the dependency is moved to the constructor instead of to methods:

public class ActionerOnA
{
    private A _a;
    private Action<A> _actionToExecute;

    public ActionerOnA(Action<A> action, A a) 
    {
        _a = a;
        _actionToExecute = action;
    }

    public ActionerOnA(Action<A> action, B b) : this(action, b.ToA()) { }
    public ActionerOnA(Action<A> action, int x, int y, int z) : this(action, new A(x, y, z)) { }
}

You would then use this like:

var actionerOnA = new ActionerOnA(Func1, a);
var actionerOnA = new ActionerOnA(Func1, b);
var actionreOnA = new ActionerOnA(Func1, x, y, z);

actionerOnA.Execute();

public void Func1(A a) { }

Upvotes: 1

Related Questions