[C#] Pointers?

Started by
13 comments, last by Stormtrooper 15 years, 9 months ago
In my game I have a ResourceManager class. If I pass an instance of this class to another class...is a copy created or does it pass a pointer? For example, say I have a test class that simply holds a string. I create an instance of the class, then pass it in the constructor of another class. If I modify the string of the class, will it change everywhere? I'm assuming it would since I'm assuming the instance and not creating a new instance.
Advertisement
classes pass by reference.
structs pass by value (copy).

strings pass by reference, but you can't modify them in place (all of the operations return a new (modified) string; leaving the original in place).
I thought that in order to pass by reference in C# you had to use the ref keyword.

[source lang = "C#"]class Bar{   public int SomeInt;   public Bar(int someint)   {      SomeInt = someint;   } //end f()} //end classclass Foo{   static void DoSomethingByVal(Bar x)   {      x.SomeInt += 5;   } //end f()   static void DoSomethingByRef(ref Bar x)   {      x.SomeInt += 5;   } //end f()   static void Main()   {      Bar bar1 = new Bar(10);      Bar bar2 = new Bar(10);      DoSomethingByVal(bar1); //bar1.SomeInt still == 10: NOT TRUE      DoSomethingByRef(bar2); //bar2.SomeInt now == 15   } //end f()} //end class


I decided to run this code before posting it and it turns out that in both cases SomeInt was increased by five. So, clearly what Telastyn said was true but what am I missing here. Why bother with the ref keyword if classes are passed by reference anyhow (or is it just for structs) and is there any to pass classes by value?
ref is mostly for value types, like structs, yes.
Ahh, thank you. Browsed MSDN a little on the subject and came up with Value types, Reference types and Passing Reference types which have gone some distance in clearing up a lot of my lingering questions about passing by value vs. passing by reference in C#.

Hopefully the links can help the OP out as well.
I'm sure those articles cover the details, but just so everyone understands the core situations, they are:

1. Passing Value Type By Value (struct without ref keyword)
2. Passing Reference Type By Value (class without ref keyword)
3. Passing Value Type By Reference (struct with ref keyword)
4. Passing Reference Type By Reference (class with ref keyword).

1.

int Clamp(int x)
{
if(x > MAX_VALUE)
x = MAX_VALUE;
return x;
}

returns x or MAX_VALUE, but does NOT change the value in the caller
changes x to MAX_VALUE if needed (affects caller)

3.

void Clamp(ref int x)
{
if(x > MAX_VALUE)
x = MAX_VALUE;
}

2.

void DisableControl(Control control)
{
control.Enabled = false;
}


disables the control that the caller passing in (affects caller)

4.

void CreateControl(ref Control control)
{
control = new Control();
}

changes the caller to have a different control instance.
----
ASSIGNMENT is the ONLY reason you would ever need to pass a reference type by reference ... this is 100% equal to passing a pointer to a pointer in C.

Case 2 and 3 are equal to each other as well, identical to a normal pointer in C.

NOTE: edited order according to ezbez's comment.

[Edited by - Xai on August 20, 2008 11:09:08 PM]
Xai, your examples aren't consistently numbered with your original points. I think they should be 1, 3, 2, 4, respectively.
Ok, thanks for the help. This is the reason I ask, maby there is a better way to do this. I have a ResourceManager class that simply holds a List<> of all the resources(textures, fonts, etc.). The controls in the GUI, entities, etc need to have a texture. The problem is releasing and reloading the textures in the DirectX device is lost and reset. Either I can pass the Resource Manager, or pass a reference of the texture to the GUI. Instead of having to go through every single control, I can simply have a method in the Resource Manager that releases and reloads all the textures...correct?
Something interesting I found is that you can't use polymorphism with references. Is there around this?

Label : Control
Container.AddControl(Label)

cannot convert from 'BrokenSilence.Gui.Controls.Label' to 'ref BrokenSilence.Gui.Base.Control'
Quote:Original post by Xai
2. Passing Reference Type By Value (class without ref keyword)

Isn't this in contradiction with what Telastyn said?

This topic is closed to new replies.

Advertisement