Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ref variable outside calling method in c#

How can I use ref variable outside the calling method in C#.

I will explain what I would like to acheive.

I have a method inside a class(MyClass), which looks like

private MyObject myobj;
public ref MyObject GetMyObject()
{
    return ref myobj;
}

Now I am fetching the variable inside another method from different class(FetchClass)

private MyObject fetchObj;
void Start()
{
    fetchObj = ref MyClass.instance.GetMyObject(); //getting error here
}

I like to use fetchObj in different method in the same class. Thats the reason I have declared outside. But I am getting error in Start method.

like image 220
Sayantan Mandal Avatar asked Nov 23 '25 10:11

Sayantan Mandal


1 Answers

Since your MyObject is a class you can use an expression bodied read-only property:

private MyObject fetchObj => MyClass.instance.GetMyObjectNotByRef();
like image 110
Guru Stron Avatar answered Nov 24 '25 23:11

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!