Is there a way to force a parameter to be passed by value instead of ref in C #, as you can in VB?

Answering only the question about calling VB6 methods with parentheses, I remembered that you can force the ByRef ByVal parameter ByVal . While studying, I found that this still works in VB.NET.

However, I cannot find anything like it in C # that allows this. Last year, I had to refer to many VB.NET class libraries that accept ByRef no good reason (believe me, I checked). This forced me to set the properties of objects to local variables in order to pass them. Not a big problem, but not very clean if you ask me.

I am wondering if there is a syntax solution that I don't know about.

As an example of my current template, which I would like to avoid:

 var tempSomeObject = BarObject.FooProperty; SomeVb6BusinessLogicMethod(ref tempSomeObject); // Continue to do work and set other temp objects due to ref constraint 

In VB6 and VB.NET, you can simply do the following to force ByVal to use the ByRef parameter.

 SomeVb6BusinessLogicMethod((BarObject.FooProperty)) 'Note the extra parens 

EDIT: I am not asking about the differences between ByRef and ByVal. I ask if C # has a similar way to get the ByRef parameter to pass ByVal. See This MSDN Document for VB.NET Features. http://msdn.microsoft.com/en-us/library/chy4288y.aspx

+6
source share
1 answer

You will need to explicitly copy the value into a new variable and pass the new variable by reference, as you showed in your question. There is no syntactic sugar in C # that would allow the compiler to execute this copy on your behalf, as is done in VB.

+5
source

Source: https://habr.com/ru/post/976512/


All Articles