1

I have a class that contains a two sets of data.

  1. A list of values for X.
  2. A list of values for Y.

Now, X and Y could hold either a string/double/integer/datetime in any possible combination. The only rule being that at any given point of time, both lists must contain equal number of values.

I could solve the problem of always holding equal data by providing access only through an AddXY method and a RemoveAt method (ensuring that at any given point of time, I can guarantee that both the X List and Y List are equally sized).

Further, I'd like the end user of this class to be able to access the X and Y values through indexers as shown below.

someClassInstance.X[i] and someClassInstance.Y[i]

Since, there are no options for this in C#, I have opted for exposing X and Y as IList (AsReadOnly) method.

Now, I considered the idea of constraining the types by using Generics. But I am unable to find appropriate examples for this particular case.

How do I say

public class MyClass<P, Q> 
     where P : Double, String, Integer, DateTime 
     and Q : Double, String, Integer, DateTime

Should I discard the idea altogether and look at some kind of Tuple or some such data structure?

EDIT: I also know that the constraints cannot be Value Types, so how does this work?

7
  • The problem is I don't want to create multiple classes for different combinations. Commented Nov 28, 2012 at 7:09
  • Why not use struct as your constraint? Commented Nov 28, 2012 at 7:10
  • How is using kvp for the set item any different than a custom collection or a tuple? Commented Nov 28, 2012 at 7:12
  • I am not suggesting that Tuples are any better. You are right in that it is no different in terms of what I am hoping to achieve. :) Commented Nov 28, 2012 at 7:15
  • After looking at your sample again I see why you an constraint on struct. It looks like you only option is to not constrain the type parameters. You might be able to use code contracts to enforce your rules. Commented Nov 28, 2012 at 7:20

1 Answer 1

4

What you are asking is not possible in C#. There is no generic type constraint that unifies these types.

The best you can do is check at runtime, for example in the static constructor. Something like this:

public class MyClass<P, Q>
{
    static MyClass() 
    {
       if (IsValidType(typeof(P) 
           && IsValidType(typeof(Q))
       throw new NotSupportedException("invalid type for MyDataStructure");
    }
    static bool IsValidType(Type type)
    {
       // logic to check whether type is acceptable
       return true;
    }
}

However I'd advise against this since it seems somewhat artificial.

Sign up to request clarification or add additional context in comments.

2 Comments

Replacing this with a code contract and turning on static checking might work.
Code contracts are useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.