Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Nullable types : by ref

Started by Emmanuel Deloget Sep 22, 2007 at 6:11 AM 0 replies 2.7k views
Original Post
Emmanuel Deloget
Emmanuel Deloget
I was trying to do something weird: I have a nullable type which encapsulate a struct (MouseState), and a function that takes a MouseState by ref:
  class Stuff
  {
    Nullable<MouseState> state = null;
    private void DoSomething(ref MouseState theState)
    { ... }
    public void Update()
    {
      if (state.HasValue)
      {
        DoSomething(ref state.Value);
      }
    }
  }
Of course, this code doesn't compile (you can't pass a value type which is the return value of a property as a ref to a function). The goal is obviously to avoid yet another a copy of the struct whne it's not null. Do I have any other way to do the same thing? Thanks for your help :)
Naurava kulkuri
Naurava kulkuri
Well, are you going to change the object itself? Like in the ref example at MSDN?

The internal values of reference types (also see Value Types (C# Reference)) can be changed even if they are passed by value. You cannot change the object itself though. You can read from Passing Reference-Type Parameters (C# Programming Guide) and perhaps start from Passing Parameters (C# Programming Guide).

So, in short, I'd say just pass the values by value since it really isn't that inefficient. It's like a shallow copy of the internal data pointers of which you can manipulate accordingly.

[EDIT]Added "see".[/EDIT]

[Edited by - Naurava kulkuri on September 22, 2007 8:50:36 AM]
---Sudet ulvovat - karavaani kulkee

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.