Allow to pass a reference of a Property using 'ref' keyword #3092
Replies: 7 comments
-
You can have For example: class MyClass
{
private Guid _guid;
public ref Guid Id => ref _guid;
} |
Beta Was this translation helpful? Give feedback.
-
@tannergooding Yes you can do that but that's not equivalent to a simple property. He's asking something different here which is just a bad idea, and that is to allow an indexer on a list to be accessed by ref. That will cause all sorts of weird issues. That said, being able to pass an auto property by ref within the scope of the type that defined the property would be awesome: class MyClass
{
public Guid Id { get; private set; }
private void SetID() => Helper.SetId(ref Id); // pass backing field by ref
} |
Beta Was this translation helpful? Give feedback.
-
@PontiacGTX I think you've confused a couple related but slightly different issues here. The line: GetLighterColor(ref Colors[index]); ...doesn't have a problem because of your You can create a ref list that does something like what you are looking for: public class RefList<T>
{
private T[] _array = new T[10];
public ref T this[int index] => ref _array[index];
}
public void SetString(ref string s) => s = "test";
var list = new RefList<string>();
SetString(ref list[5]);
Console.WriteLine(list[5]); // Outputs "test" Breaking changes aside, the normal I kind of wish C# supported the VB way of passing properties by ref. VB copies the property value into a temp variable, passes it by ref, then writes the value back to the property. This can lead to what some people consider unexpected behavior in certain situations but I think the tradeoff is well worth it and the behavior is really not that hard to wrap your head around. APIs that need to get and set a property value could be greatly simplified if this was possible. I don't know if the VB way supports passing indexers by ref the same way (I imagine it probably does), but the same approach could be used to allow that as well: var list = new List<string>() { null };
SetString(ref list[0]);
// Compiles to:
var list = new List<string>() { null };
string value = list[0];
SetString(ref value);
list[0] = value; What I definitely think should be allowed is the example in my prior post for auto-properties where the backing field can just be passed by ref as that works how you would expect. |
Beta Was this translation helpful? Give feedback.
-
There's a proposal to add ref indexer-like functionality to |
Beta Was this translation helpful? Give feedback.
-
@svick I meant as a replacement for the normal indexer, which is why I mentioned that being a breaking change. I can see that being useful as an "unsafe" side API with different behavior and some caveats for performance optimizing, as your link shows. I haven't seen that before though or I would have mentioned it in my previous response, so thanks for the neat find :) |
Beta Was this translation helpful? Give feedback.
-
@svick It doesn't look like that proposal is happening, but we do have this approved and merged which lets you get a span from a list which you can then ref into: https://github.com/dotnet/corefx/issues/31597 @PontiacGTX You can use the functionality added in the related PR to do this: GetLighterColor(ref CollectionsMarshal.AsSpan(Colors)[index]); |
Beta Was this translation helpful? Give feedback.
-
In the case of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Currently C# developers are becoming used to writing a autoimplemened property without a second thought that they should use a backing field for storing their data, and classes could contain many declared autoimplemented properties to be later used as a field rather a property with getter and setter function,Some developers who might take some old code which contain these kind of classes would find out that when they are going to pass what should be a field as a reference to a function,they cant do it because properties cant be passed with out or ref keyword.
some simple example of this issue could be:
Beta Was this translation helpful? Give feedback.
All reactions