ref

How can I emit a dynamic method returning a ref?

冷暖自知 提交于 2019-12-05 06:58:50
I'm navigating the ins and outs of ref returns, and am having trouble emitting a dynamic method which returns by ref. Handcrafted lambdas and existing methods work as expected: class Widget { public int Length; } delegate ref int WidgetMeasurer(Widget w); WidgetMeasurer GetMeasurerA() { return w => ref w.Length; } static ref int MeasureWidget(Widget w) => ref w.Length; WidgetMeasurer GetMeasurerB() { return MeasureWidget; } But emitting a dynamic method fails. Note : I'm using Sigil here. Apologies, I'm less familiar with System.Reflection.Emit . WidgetMeasurer GetMeasurerC() { FieldInfo

Ref Abuse: Worth Cleaning Up?

為{幸葍}努か 提交于 2019-12-05 02:15:12
I have inherited some code that uses the ref keyword extensively and unnecessarily. The original developer apparently feared objects would be cloned like primitive types if ref was not used, and did not bother to research the issue before writing 50k+ lines of code. This, combined with other bad coding practices, has created some situations that are absurdly dangerous on the surface. For example: Customer person = NextInLine(); //person is Alice person.DataBackend.ChangeAddress(ref person, newAddress); //person could now be Bob, Eve, or null Could you imagine walking into a store to change

In C#, where do you use “ref” in front of a parameter?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 09:02:37
问题 There are a number of questions already on the definition of "ref" and "out" parameter but they seem like bad design. Are there any cases where you think ref is the right solution? It seems like you could always do something else that is cleaner. Can someone give me an example of where this would be the "best" solution for a problem? 回答1: In my opinion, ref largely compensated for the difficulty of declaring new utility types and the difficulty of "tacking information on" to existing

Is this std::ref behaviour logical?

旧城冷巷雨未停 提交于 2019-12-04 08:46:27
问题 Consider this code: #include <iostream> #include <functional> int xx = 7; template<class T> void f1(T arg) { arg += xx; } template<class T> void f2(T arg) { arg = xx; } int main() { int j; j=100; f1(std::ref(j)); std::cout << j << std::endl; j=100; f2(std::ref(j)); std::cout << j << std::endl; } When executed, this code outputs 107 100 I would have expected the second value to be 7 rather than 100. What am I missing? 回答1: A small modification to f2 provides the clue: template<class T> void f2

Why can I ref return an item of an array that only exists inside the method?

六眼飞鱼酱① 提交于 2019-12-04 03:27:21
I was trying out the new ref returns of C#7. I am able to compile and build this: public ref string MisUseRefReturn(int index) { string[] array = { "a", "b", "c", "d" }; return ref array[index]; //array[2] gets out of scope when this method returns! } According to MSDN: The return value cannot be a local variable in the method that returns it; it must have a scope that is outside the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to the method. Attempting to return a local variable generates compiler error CS8168, "Cannot return local

Alternative to using ref in foreach?

情到浓时终转凉″ 提交于 2019-12-04 03:12:27
问题 I have a modifying method with a signature like private bool Modify(ref MyClass obj); that will make modifications to obj and indicate succes with it's return value. Modify is not reassigning the reference (I know that this wouldn't work), just modifying instance fields, so I want to use it to do something like the following: foreach(MyClass obj in myList) { bool success = Modify(obj); // do things depending on success } I am running into a problem compiling as obj is "not being passed with

Example of practical of “ref” use [closed]

[亡魂溺海] 提交于 2019-12-04 03:05:42
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am struggling how to use "ref" (to pass argument by reference) in real app. I would like to have simple and mainly meaningful example. Everything I found so far could be easily redone with adding return type to the method. Any idea someone? Thanks! 回答1: The best example coming

ref for variables not parameters in functions

假如想象 提交于 2019-12-03 22:53:29
Suppose I have a Person class and have the following: Person A = new Person("Tom"); Person B = A; Is there a way I can change it so that if I assign a new Person to B , B = new Person("Harry") , A would be referring to the same instance too? I know you can do that in a ref parameter assignment in a function. UPDATE: The feature described here was added to C# 7. The feature you want is called "ref locals" and it is not supported in C#. The CLR does support generating code that contains ref locals, and a few years ago I wrote an experimental version of C# that had the feature you want, just to

Boxing and unboxing when using out and ref parameters

别来无恙 提交于 2019-12-03 22:41:50
Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType? For ref Keyword Its already mentioned on MSDN that : Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference. As for out keyword: The out keyword causes arguments to be passed by reference . This is like the ref keyword, except that ref requires that the variable be

Why can't iterator methods take either 'ref' or 'out' parameters?

这一生的挚爱 提交于 2019-12-03 03:33:28
问题 I tried this earlier today: public interface IFoo { IEnumerable<int> GetItems_A( ref int somethingElse ); IEnumerable<int> GetItems_B( ref int somethingElse ); } public class Bar : IFoo { public IEnumerable<int> GetItems_A( ref int somethingElse ) { // Ok... } public IEnumerable<int> GetItems_B( ref int somethingElse ) { yield return 7; // CS1623: Iterators cannot have ref or out parameters } } What's the rationale behind this? 回答1: C# iterators are state machines internally. Every time you