K
kndg
Hi all,
I know that C# does support a pointer to a pointer type but that only
works on a value types. As far as I know, C# does not support a
reference to a reference type (I don't know a correct word but what I
mean here is you cannot store an address to a reference type -- am I
correct?). Hopefully below code example explains what I am trying to do.
I have been searching the internet for a workaround but couldn't find a
good answer. So, I made my own wrapper class...
using System;
public class Reference<T>
{
private Func<T> pointer;
public T Value
{
get { return pointer(); }
}
public Reference(Func<T> pointer)
{
this.pointer = pointer;
}
}
public class Test
{
public static void Main()
{
string str1 = "Hello";
string str2 = "World";
var ref1 = new Reference<string>(() => str1);
var ref2 = new Reference<string>(() => str2);
str1 = "Hi";
Console.WriteLine(ref1.Value);
Console.WriteLine(ref2.Value);
}
}
The above code works, but I'm hoping something that is much more
simpler. The above class takes a lambda as method parameter which has a
drawback (the file size will grow proportionally with the number of
variables used) though it is not something that I'm worried about. I had
tried replacing the method signature to make it pass by reference but
couldn't make it to work.
public Reference(ref T pointer)
{
// do something
}
I know the above code smells a bad design, but I couldn't find an
alternative to my situation. I am working on a data structure
(specifically: a HashSet) that store some objects. Some of these objects
are not initialized during start-up and have the null value. These
object will later initialized based on the user input. If I add these
objects to the HashSet, only the first one will be added, while the rest
are discarded, which is not what I want. I had tried make a wrapper
class for this object but it make the code looks more complicated than
it supposed to be.
Does anyone here have an advice?
Regards.
I know that C# does support a pointer to a pointer type but that only
works on a value types. As far as I know, C# does not support a
reference to a reference type (I don't know a correct word but what I
mean here is you cannot store an address to a reference type -- am I
correct?). Hopefully below code example explains what I am trying to do.
I have been searching the internet for a workaround but couldn't find a
good answer. So, I made my own wrapper class...
using System;
public class Reference<T>
{
private Func<T> pointer;
public T Value
{
get { return pointer(); }
}
public Reference(Func<T> pointer)
{
this.pointer = pointer;
}
}
public class Test
{
public static void Main()
{
string str1 = "Hello";
string str2 = "World";
var ref1 = new Reference<string>(() => str1);
var ref2 = new Reference<string>(() => str2);
str1 = "Hi";
Console.WriteLine(ref1.Value);
Console.WriteLine(ref2.Value);
}
}
The above code works, but I'm hoping something that is much more
simpler. The above class takes a lambda as method parameter which has a
drawback (the file size will grow proportionally with the number of
variables used) though it is not something that I'm worried about. I had
tried replacing the method signature to make it pass by reference but
couldn't make it to work.
public Reference(ref T pointer)
{
// do something
}
I know the above code smells a bad design, but I couldn't find an
alternative to my situation. I am working on a data structure
(specifically: a HashSet) that store some objects. Some of these objects
are not initialized during start-up and have the null value. These
object will later initialized based on the user input. If I add these
objects to the HashSet, only the first one will be added, while the rest
are discarded, which is not what I want. I had tried make a wrapper
class for this object but it make the code looks more complicated than
it supposed to be.
Does anyone here have an advice?
Regards.