Question on C# Interface Method Parameters

Hello everyone, I was doing some work in trying to familiarize myself with FMS’, and in one of the tutorials I was looking at I saw something similar to:

public interface IState {
	//test interface used in a FFSM.
	void Update (DummyClass target);
}

Now this was all fine and good, but I was left wondering about 2 things:

  1. Is there a way to implement some sort of generic parameters for the method? Something like:

    public interface IState {
    //test interface used in a FFSM.
    void Update (AnyClass anythingIWant);
    }

→ For a case where you either don’t know what type you’ll end up working with or just want a large variety of types to be useable.

  1. Is there a way to overload the parameters? Something like:

    public interface IState {
    //test interface used in a FFSM.
    void Update (DummyClass target);
    void Update ();
    }

→ Since each time I add a method, I have to keep adding implementations that may not make any sense in the class that implements them.

Any insights are appreciated, and I apologize if my questions are outright stupid. [Also, anyone else have the TAGS for posts keep disappearing when entering new ones? Really annoying…]

There are a few different approaches to your first question:

void Update (AbstractDummyClass anythingIWant);
void Update (IDummyClass anythingIWant);

public interface IState<T>
{
    void Update(T target);
}

I’m not really sure what you’re asking with the second question. You can always overload interface methods in the classes where it makes sense:

public interface IState
{
    void Update (DummyClass target);
}

public class MyState
{
    public void Update (DummyClass target) {};
    public void Update (SomethingElse target) {};
}

You could even chain interfaces:

public interface IState
{
    void Update (DummyClass target);
}

public interface ISpecificState : IState
{
    void Update (SomethingElse target);
}

public class MyState : ISpecificState
{
    public void Update (DummyClass target) {};
    public void Update (SomethingElse target) {};
}

Take a look to generic methods. Link to MSDN. It has your method accepting any kind of class.

For the no parameters part of your question you could make it null by default:

public interface IState {
    //test interface used in a FFSM.
    void Update (DummyClass target = null);
}

That will be OK calling it as “Update()” so in that case the parameter target would be null.

i’d just like to throw this in to see if it helps:

    public interface IState {
    //test interface used in a FFSM.
    void Update (DummyClass target);
    }

    public class State : MonoBehaviour, IState
    {
        void Update() {
        // If you want to call Update(DummyClass)
            Update(default(DummyClass));
        }
        void Update(DummyClass dummy)
        {
            // do something with DummyClass
        }
    }

Note the use of default(DummyClass) assigning null to a Complex Type is invalid so use default instead which just so happens to return null in this case.