Dear C# experts,
Is there any way to parametrize generics with value type reference.
I want to do something like this :
myObject.ID = 1;
List myInts = new List();
myInts.add(ref myObject.ID);
myInts[0] = 2;
Assert.IsTrue(myObject.ID == 2);
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Sep 15, 2008, 5:36 AM
Not really.
The nearest you can get (assuming MyObject is a reference type) is:
myObject.ID = 1; myInts = new List();
List
myInts.Add(myObject);
myInts[0].ID = 2;
Assert.IsTrue(myObject.ID == 2); //true
clementPosted Sep 15, 2008, 7:19 PM
It's a shame we don't get the same flexibility as in C with pointers (list of int*).
I find it especially annoying for immutable types like string in c# : it means we can't modify
string references through a list.