Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by The-Oddler · Apr 08, 2011 at 11:48 AM · objectclassstruct

Returning a copy of a struct - c#

Hi,

I have a script that manages different types of tiles, all info of a tile is grouped together in a struct "TileInfo". Now I have a method:

public static TileInfo GetTileInfo(int id)
{
return TileInfoList.GetTileInfo(id);
//Looks up the TileInfo var with ID == id and returns the correct struct
}

Now my question, will this return a copy of that struct, or the actual struct itself ? So when I use this method, and change the returned struct, will the original change ? If so, how do I return a copy so my original struct stays the way he was ? If not, just out of curiosity, how do I make it so it returns the actual struct ? :P (I need it to return a copy, but it's still interesting :P)

(I'm not sure if I'm saying it right "returning the actual struct", or is there a word for an instance of that struct (like "object" when we're talking about classes) or is it also just called an "object" ? :D)

Thanks !

-Pablo

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Proclyon · Apr 08, 2011 at 12:13 PM 0
Share

You want to return an object reference on constructor call?

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Chewbacca · Apr 08, 2011 at 01:31 PM

structs are value types. As such the function will return a copy of that struct and NOT the actual struct itself.

See http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx for more info.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The-Oddler · Apr 08, 2011 at 01:55 PM 0
Share

Thanks ! Perfect answer :P

avatar image
4
Best Answer

Answer by Dani · Apr 08, 2011 at 01:45 PM

Structs are always copied, classes are always referenced

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The-Oddler · Apr 08, 2011 at 01:56 PM 0
Share

Thanks :D $$anonymous$$y for you Chewbacca was first, and apparently I can only select one answer as the correct one :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is it possible to Change mesh also multi materials by a button or a key 0 Answers

Trouble with an array of classes - Cannot implicitly convert type 4 Answers

Type 'Object' does not support slicing 1 Answer

Best way to store a lot of data by script 1 Answer

Getting any type in Unity Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges