Why does the check for an interface fail?

Posted by chobo on Stack Overflow See other posts from Stack Overflow or by chobo
Published on 2010-05-18T21:00:24Z Indexed on 2010/05/18 21:10 UTC
Read the original article Hit count: 149

Filed under:

I have a class that implements an interface. In another area of the code I check if that class instance contains that interface, but it doesn't work. The check to see if the class contains the interface always fails (false) when it should be true.

Below is a simple representation of what I am trying to accomplish.

Example

  public interface IModel
    {
        bool validate();
    }


  public class SomeModel : IModel
    {
        public SomeModel
        {
        }

        public bool Validate()
        {
            return true;
        }        
    }



    // Dummy method
    public void Run()
    {
        SomeModel model = new SomeModel();

        if (model is IModel)
        {
           string message = "It worked";
        }
        else
        {
            string message = "It failed";
        }
    }

© Stack Overflow or respective owner

Related posts about c#