1

Interface :

public interface IArrayOperation
{
    int GetElement(int index);        
    bool IndexCheck(int index);
}

Static Class:

public static class TestArray
{
    public static int GetArrayLength(IArrayOperation arrayOperation)
    {
        // Implement your logic here.
        // I need to implement interface method over here.
        throw new NotImplementedException();
    }
}

Here, I want to implement both interface methods in the static class method GetArrayLength().

I don't want to implement interface but I have passed the interface as a parameter in the static class method.

Appreciated any help or guidance.

Deepak Kushvah
  • 278
  • 2
  • 12
  • 2
    Possible duplicate of [Why can static classes not implement interfaces?](http://stackoverflow.com/questions/1266422/why-can-static-classes-not-implement-interfaces) – Alex Feb 02 '16 at 11:48
  • Thanks Stuartd. You are correct. I want to get the Virtual Array length in the static class by using the interface methods. – Deepak Kushvah Feb 02 '16 at 11:49
  • @Stuartd why did you re-open the question, it obviously IS a duplicate of the quoted one? – MakePeaceGreatAgain Feb 02 '16 at 11:50
  • You must pass an instance of a class implementing `IArrayOperation` to `GetArrayLength`. – stuartd Feb 02 '16 at 12:07
  • Thanks Stuartd. But I can't change the definition of the "GetArrayLength()" method. – Deepak Kushvah Feb 02 '16 at 12:31
  • 1
    "I want to implement both interface methods … I don't want to implement interface" Which? – Jon Hanna Feb 02 '16 at 13:24
  • You don't implement methods within other methods, so your requirement makes no sense. Perhaps you should show what you _want_ to do (even if it isn't valid syntax) so your question makes more sense? Do you mean that you want to _call_ the methods on the interface from within that static method? – D Stanley Feb 02 '16 at 16:25

1 Answers1

0

You cannot implement interface methods without having a derived class. However, you can add derived information to an interface by means of extension methods, if your interface provides enough basic functionality.

For an array, you could have the interface method IndexCheck and derive the array length by checking for the last valid index.

public interface IArrayOperation
{       
    bool IndexCheck(int index);
}
public static class TestArray
{
    public static int GetArrayLength(this IArrayOperation arrayOperation)
    {
        int len = 0;
        while (arrayOperation.IndexCheck(len)) { ++len; }
        return len;
    }
}

Or you could have an array length and derive the index check

public interface IArrayOperation
{       
    int GetArrayLength();
}
public static class TestArray
{
    public static bool IndexCheck(this IArrayOperation arrayOperation, int index)
    {
        return index >= 0 && index < arrayOperation.GetArrayLength();
    }
}

In both cases, you can later use an IArrayOperation variable with both methods

IArrayOperation instance = /* some concrete derived class */;
bool checkResult = instance.IndexCheck(0);
int lengthResult = instance.GetArrayLength();

Your derived class instances need to implement the methods that are actually part of the interface, but the extension methods are available without implementation per instance.

grek40
  • 13,113
  • 1
  • 24
  • 50