Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Interface with Either Number or DateTime
    text
    copied!<p>My problem can be simplified to a List&lt;> with X and Y coordinates that I'd like to group into an interface or abstract class. I then have a bunch of other functions that take this abstract class as an argument. The caveat to this is that the X element is either of a numerical or is a DateTime. Here's some sample code to illustrate what I'm trying to do:</p> <pre><code>//C# 4.0 abstract class Element : Group&lt;DateTime, double&gt; { public DateTime X {get; set;} public double Y {get; set;} } public interface Group&lt;T, U&gt; { public T X {get; set;} public U Y {get; set;} } void OtherObject.Do(List&lt;Group&lt;DateTime, double&gt;&gt; elm) { double[] X = 0; for (int i = 0; i &lt; elm.Count; i++) { if (elm[i].GetType() == typeof(DateTime)) { X[i] = ((DateTime)elm[i].X).TOADate(); { else { X[i] = elm.X; } } //Other stuff } static Main() { List&lt;Element&gt; list = FactoryCreate(); OtherObject obj = new OtherObject(); obj.Do(list); } </code></pre> <p>I'd like to be able to specify the <code>List&lt;Group&lt;DateTime, double&gt;&gt;</code> argument to <code>Do()</code> without specifying that the generic types. I'd just like to be able to do <code>Do(List&lt;Group&gt; lst)</code>. </p> <p>I <em>could</em> just eliminate the generic type parameters and use the dynamic data type in Group but I don't feel like thats the best way. </p> <p>Is there an established pattern to deal with this issue? I'm 100% open to suggestions as to the correct way to model this data structure/class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload