Generics Questions

  • Thread starter Thread starter csharpvista
  • Start date Start date
C

csharpvista

how you would create a “Pair†class that uses generics to group an unrelated
pair of classes such that it would be instantiated as:
Pair<MyClass1, MyClass2> pair = new Pair<MyClass1, MyClass2>(first,
second);


Thanks
 
csharpvista said:
how you would create a “Pair†class that uses generics to group an
unrelated
pair of classes such that it would be instantiated as:
Pair<MyClass1, MyClass2> pair = new Pair<MyClass1, MyClass2>(first,
second);

You can simply set "first" and "second" by means of the constructor,
unless I am completely misunderstanding what you really want.

public class Pair<T1, T2>
{
public T1 First { get; set; }
public T2 Second { get; set; }

public Pair<T1, T2>(T1 first, T2 second)
{
First = first;
Second = second;
}
}
 
csharpvista said:
how you would create a “Pair†class that uses generics to group an
unrelated
pair of classes such that it would be instantiated as:
Pair<MyClass1, MyClass2> pair = new Pair<MyClass1, MyClass2>(first,
second);

You can simply set "first" and "second" by means of the constructor,
unless I am completely misunderstanding what you really want.

public class Pair<T1, T2>
{
public T1 First { get; set; }
public T2 Second { get; set; }

public Pair<T1, T2>(T1 first, T2 second)
{
First = first;
Second = second;
}
}
 
Thank you very much!
I am just now learning Generics and came across this question.
Thanks again.
 
Thank you very much!
I am just now learning Generics and came across this question.
Thanks again.
 
csharpvista said:
how you would create a “Pair†class that uses generics to group an unrelated
pair of classes such that it would be instantiated as:
Pair<MyClass1, MyClass2> pair = new Pair<MyClass1, MyClass2>(first,
second);


Thanks

System.Collection.Generic defines class KeyValuePair<KeyType,
ValueType>. It is used on key based collections like
SortedDictionary<KeyType, ValueType> and SortedList<KeyType, ValueType>
to add or fetch key/value pairs. I don't know if there is any restict on
the KeyType, i.e. can you use your MyClass1.
 
csharpvista said:
how you would create a “Pair†class that uses generics to group an unrelated
pair of classes such that it would be instantiated as:
Pair<MyClass1, MyClass2> pair = new Pair<MyClass1, MyClass2>(first,
second);


Thanks

System.Collection.Generic defines class KeyValuePair<KeyType,
ValueType>. It is used on key based collections like
SortedDictionary<KeyType, ValueType> and SortedList<KeyType, ValueType>
to add or fetch key/value pairs. I don't know if there is any restict on
the KeyType, i.e. can you use your MyClass1.
 
System.Collection.Generic defines class KeyValuePair<KeyType,
ValueType>. It is used on key based collections like
SortedDictionary<KeyType, ValueType> and SortedList<KeyType, ValueType>
to add or fetch key/value pairs. I don't know if there is any restict on
the KeyType, i.e. can you use your MyClass1.

Since KeyValuePair.Key and KeyValuePar.Value are readonly properties
it may not meet his requirements.
 
System.Collection.Generic defines class KeyValuePair<KeyType,
ValueType>. It is used on key based collections like
SortedDictionary<KeyType, ValueType> and SortedList<KeyType, ValueType>
to add or fetch key/value pairs. I don't know if there is any restict on
the KeyType, i.e. can you use your MyClass1.

Since KeyValuePair.Key and KeyValuePar.Value are readonly properties
it may not meet his requirements.
 
Back
Top