tags:

views:

133

answers:

4

This is probably not possible, but here goes:

I want to create a struct where I can define the amount of arguments at declaration.

for example, now I am using:

KeyValuePair<T, T>

but KeyValuePair can only ever take a Key , and a Value .

Is it possible to make something like:

CustomValues<T, {T, {..}}>

I think this isn't possible, but maybe I just don't know enough C#. I'm also open to clever workarounds,

Thank you

+1  A: 

But, your KVP value type can be a generic one as well:

KeyValuePair<T, List<U,V>>

OR:

KeyValuePair<T, KeyValuePair<U,V>>
Oded
I want to create something that the Junior programmers can use easily, instead of giving them a choice to do whatever comes to their mind first
Theofanis Pantelides
+3  A: 

No, this isn't possible, as showcased by Func<T>, Func<T, TResult>, Func<T1, T2, TResult>, etc.

Mark Seemann
So essentially, just have multiple declarations? with 1-10 arguements?
Theofanis Pantelides
Well, that, or consider whether it's actually a good way to model your API at all. Often it isn't. However, in .NET 4, we get lots of Tuple types just like that.
Mark Seemann
I know it's terrible logic, but what I want to avoid is having loads and loads of custom structs created for minuscule tasks, such as passing SQL results back and forth in functions. I know LINQ solves this, but 2.0 for now
Theofanis Pantelides
public struct CustomValues<A> { public A a; } public struct CustomValues<A, B> { public A a; public B b; } public struct CustomValues<A, B, C> { public A a; public B b; public C c; } public struct CustomValues<A, B, C, D> { public A a; public B b; public C c; public D d; }
Theofanis Pantelides
It sometimes feels really painful to have to make a new custom type just to pass related values around, but it's still the best option, because it forces us to think about modeling. Next thing you know, such structures can implement behavior as well. It's a better option for OO design.
Mark Seemann
Absolutely no disagreement there.
Theofanis Pantelides
+1  A: 

You may try this

public class CustomClass
    {
     KeyValuePair<T , KeyValuePair<T, V>> setOfArguments;
     public CustomClass(KeyValuePair<T, KeyValuePair<T, V>> _setOfArguments)
      {
        setOfArguments = _setOfArguments;
      }       
    }
Asad Butt
A: 

Will this work for you?

public void UseParams(params KeyValuePair<String, String>[] list) {
     ...
}
Naeem Sarfraz
No, I don't want an Array of KeyValuePair(s), I want to make my own custom KeyValuePair, where I can pass in as many arguments as I want
Theofanis Pantelides