Jump to content
  • Advertisement
ethancodes

C# Question about reference types

Recommended Posts

I feel like I should know this, but I tried looking it up to make sure I wasn't confusing myself, and couldn't seem to find a clear answer that made sense to me. When assigning something like this:

GridSquare randomPosition;

randomPosition = gridPositions[Random.Range(0,gridPositions.GetLength(0)),Random.Range(0,gridPositions.GetLength(1))]; 

Am I putting a copy of the GridSquare in gridPositions into the GridSquare randomPosition, or is randomPosition referencing the GridSquare at this location? If it's a copy, is there a way to reference it, like a pointer in c++, because that's what I want to do.

Share this post


Link to post
Share on other sites
Advertisement
Just now, masskonfuzion said:

You said "like c++" which suggests that your example code isn't c++. So... What language is it?

I'm sorry, it's c#. I guess I forgot to tag it.

Share this post


Link to post
Share on other sites
1 minute ago, ethancodes said:

gridPositions is type GridSquare and GridSquare is a class.

So you're saying that GridSquare has an index operator that returns another GridSquare?
 

public class GridSquare
{
   public GridSquare this[int x, int y]
   {
       returns ?????
   }
}

if GridSquare is a class then it is a reference type and unless you are doing something funky in the index operator (e.g. constructing a new GridSquare) then it will most likely return a reference to an existing object.

Share this post


Link to post
Share on other sites

I'm not sure what you mean with your above code? But in any case, you answered my question. I am using randomPosition to find a GridSquare from my array, and randomPosition is a reference to that GridSquare, not a copy of it. Thank you.

Share this post


Link to post
Share on other sites
5 hours ago, ethancodes said:

I'm not sure what you mean with your above code?

1

you said

17 hours ago, ethancodes said:

gridPositions is type GridSquare

I thought that seemed kind of odd, but what you meant was gridPositions is an array of GridSquares. That might seem pedantic, but it's an important detail.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!