- Home /
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
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
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.
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`
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, ...
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".
Woooow really good explanation I didn't know that one bit.
Thank you very much.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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