want to pass value using ref in c# but its not working like I think it should <p>Maybe what I want to do is just the wrong way of going about it. But i am trying to write a game debug printer thing. It is to write values to the screen in a sprite font for various things i tell the debugger to care about. My debugger class is simple , but it seems that in c# things are getting passed by value not reference. So I turn to the ref keyword, which seems to work okay if I pass in the right data type. The issue is that I have this method signature:</p> <pre><code>Dictionary&lt;String, object&gt; debugStrings = new Dictionary&lt;String, object&gt;(); ... ... public void addVariable(String index, ref object obj) //i think it needs to be a reference, not so sure though. { debugStrings.Add(index, obj); } </code></pre> <p>This adds to the dictionary a variable to ultimately print to the screen along with a key for it to be known as.</p> <p>The problem though arises when I try to use the above method:</p> <pre><code>debugPrinter.addVariable("myrotationvalue", ref this.Rotation); </code></pre> <p>as per the link in the comment I changed the code above to:</p> <pre><code>this.Rotation = 4; object c = (object)this.Rotation; this.Rotation = 20; level.dbgd.addVariable("playerrot", ref c); //always prints 4 out, i guess it still is not passing by reference? </code></pre> <p>So it doesn't give an error but always prints out 4. Not sure how I will ever get the reference to work.</p> <p>Again on another edit took the references out:</p> <pre><code>this.Rotation = 20; level.dbgd.addVariable("playerrot", this.Rotation); this.Rotation = 4; //should draw to screen 4, doesn't draws 20 </code></pre> <p>I apparently this is harder than I thought it would be a simple little fun class to work up before I hit the hay, ha ha ha... this is not boding well for the monday to come.</p> <p>Full class:</p> <pre><code>namespace GameGridTest.GameGridClasses.helpers { public class DebugDrawer : DrawableGameComponent { Dictionary&lt;String, object&gt; debugStrings = new Dictionary&lt;String, object&gt;(); int currentX; int currentY; VictoryGame _game; private SpriteBatch spriteBatch; private SpriteFont spriteFont; public DebugDrawer(VictoryGame game) : base(game) { _game = game; currentX = _game.Window.ClientBounds.Width - 400; //draw the debug stuff on right side of screen currentY = 5; } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); spriteFont = _game.Content.Load&lt;SpriteFont&gt;("fonts\\helpers\\fpsFont"); } public void addVariable&lt;T&gt;(String index, AbstractDebugHandler&lt;T&gt; obj) //i think it needs to be a reference, not so sure though. { debugStrings.Add(index, obj); } public void removeVariable(String index) { debugStrings.Remove(index); } protected override void UnloadContent() { _game.Content.Unload(); } public override void Update(GameTime gameTime) { } public override void Draw(GameTime gameTime) { spriteBatch.Begin(); foreach (object obj in debugStrings) //draw all the values { spriteBatch.DrawString(spriteFont, obj.ToString(), new Vector2(currentX, currentY), Color.White); currentY += 30; } currentY = 5; spriteBatch.End(); } } public abstract class AbstractDebugHandler&lt;T&gt; { public AbstractDebugHandler(T obj) { InnerObject = obj; } public T InnerObject { get; private set; } public abstract string GetDebugString(); } public class ThisDebugHandler: AbstractDebugHandler&lt;object&gt;{ public ThisDebugHandler(object innerObject) : base(innerObject){ } public override GetDebugString(){ return InnerObject.Rotation; //?? } } } </code></pre>