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
0
Question by sdgd · Feb 24, 2014 at 03:13 PM · c#listoafa

Why can't I give pointer to Collections.Generic.List<>

why can't I pass A property or indexer as an out or ref parameter?

 System.Collections.Generic.List<Transform> TestList = new System.Collections.Generic.List<Transform>();
 void Start(){
     TestList.Add(transform);
     Test(ref TestList[0]);
 }
 void Test(ref Transform t){
     
 }

what I get is:

 Assets/Scripts/Tests/TestScript.cs(9,26): error CS0206: A property or indexer may not be passed as an out or ref parameter

 Assets/Scripts/Tests/TestScript.cs(9,17): error CS1502: The best overloaded method match for `TestScript.Test(ref UnityEngine.Transform)' has some invalid arguments

 Assets/Scripts/Tests/TestScript.cs(9,17): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Transform'


it goes through IF I put it in to:

 Transform t = TestList[0];

  • well resault would be same BUT problem is because I'm trying to pass a structure and not class.

  • also resault would be same without ref, but not for structure.

while I was experimenting I found that I can't even do with class, ... and what I don't get is why not?

oh, ... and could someone add tag: cs0206

Comment
Add comment · Show 4
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 RickyAh · Feb 24, 2014 at 03:38 PM 0
Share

I just want to note that as the type Transform is not a Value Type, but a Reference Type, it will always get passed by reference. ref keyword only applies to Value Types: http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

Use Type.IsValueType property if you want to know if a type is a Value Type

e.g. bool result = (typeof(Transform).IsValueType); // result is false

avatar image sdgd · Feb 24, 2014 at 03:41 PM 0
Share

ok know it's not value type, ... that's why I started to experiment with reference types, ...

at first I was trying with my custom structure and got same errors, ...

than I experimented and did this code and post it.

avatar image RickyAh · Feb 24, 2014 at 04:57 PM 0
Share

I think I don't understand you. What I meant with my previous post is that just doing this works and you would be passing TestList[0] by reference.

 System.Collections.Generic.List<Transform> TestList = new   System.Collections.Generic.List<Transform>();
 void Start(){
     TestList.Add(transform);
     Test(TestList[0]);
 }
 void Test(Transform t){
  
 }`enter code here`
avatar image sdgd · Feb 24, 2014 at 05:21 PM 0
Share

yes that exact thing was bugging me, because it's reference type it goes by reference but IF I type ref it can't well thanks to answer now I understand that, ...

1 Reply

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

Answer by CHPedersen · Feb 24, 2014 at 03:31 PM

This happens precisely because of what the error message says. A list is not an array, it is instead a wrapper around a native array (which just encapsulates the resizability for you). Thus, a native array is the only data structure that allows for direct access to its elements through brackets '[]'. A List actually doesn't naturally come with that ability - it only allows you to do so anyway because the class implements a so called indexer, which is a C# language feature that allows you to custom define what bracket-syntax does for a class. You can see an example of how to implement an indexer on MSDN's website here:

http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

That example is probably pretty close to what the actual List implementation looks like. But because the indexer syntax in a class definition is just syntactic sugar around get/set accessors that take an index as argument, you cannot use the 'ref' parameter with them. C# reads that as "a reference to a get method" (the indexer's 'get' part), where you expected it to read as "a reference to the item at index 0". And then the compiler throws the error you see: "A property or indexer may not be passed as an out or ref parameter".

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 sdgd · Feb 24, 2014 at 03:38 PM 0
Share

Woooow really good explanation I didn't know that one bit.

Thank you very much.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Make Lists within a List 0 Answers

Confused about copying Lists! 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