Specific types in implementing class when using an interface
<p>Consider the following code sample:</p>

<pre><code>interface IData {
  int Count();
}

interface IOperations {
  IData Foo();
  double Bar(IData a);
}

class Data1 : IData {
  public int Count() { return 37; }
  public double SomethingElse { get; set; }
}

class Ops1 : IOperations 
{
  public Data1 Foo() { return new Data1(); } // want to return specific type here
  public double Bar(Data1 x) { ... } // want to get specific type here
                                     // and not use operator as everywhere
}

// more definitions of classes Data2, Ops2, Data3, Ops3, ...

// some code:
Ops1 a = new Ops1();
Data1 data = a.Foo(); // want Data1 here not IData!
double x = a.Bar(data);
</code></pre>

<p>I could of course just use <code>public IData Foo() { return new Data1(); }</code>:</p>

<pre><code>// some code
Ops1 a = new Ops1();
Data1 data = a.Foo() as Data1;
</code></pre>

<p>but with <code>as</code> everywhere, the code is quickly becoming confusing.</p>

<p>I wonder if there is a good design pattern to achieve this in a clear and strong way?</p>

<p><strong>Edit:</strong> Is is important, that Ops and Data share a common base class:</p>

<pre><code>List&lt;IOperations&gt; ops = ...;
List&lt;IData&gt; data = ...;
List&lt;double&gt; result = ...;
for(int i=0; i&lt;ops.Count; i++) 
  result[i] = ops[i].Bar(data[i]);
</code></pre>

<p>So for the case with the return type, I wonder that this is forbidden, because I satisfy the requirement of the interface. In the case with parameters probably there is some additional (template) layer required.</p>
