ref

Recover containing GC object from managed 'ref' interior pointer

无人久伴 提交于 2019-12-01 19:56:46
This question is newly relevant in light of the new ref locals and ref return features in the latest versions of C# 7 : With the increased prominence and wider use of managed--or "interior"--pointer variables in C#, occasionally you may need to recover the respective containing Pinnable GC object for such a pointer. For example, if you are passing around a managed pointer to an array element of type T , you might need the array reference T[] itself in order to call (e.g.) Array.Copy(...) . So, from managed code, is there any reasonably legitimate way to recover the containing GC object handle,

Alternative to using ref in foreach?

怎甘沉沦 提交于 2019-12-01 16:34:06
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 the ref keyword". However, if I put the ref keyword in like so: bool success = Modify(ref obj); I get

Example of practical of “ref” use [closed]

我们两清 提交于 2019-12-01 16:13:17
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! The best example coming in my mind is a function to Swap two variables values: static void Swap<T>(ref T el1, ref T el2) { var mem = el1; el1 = el2; el2 = mem; } Usage: static void Main(string[] args) { string a = "Hello"; string b = "Hi"; Swap(ref a, ref b); // now a = "Hi" b = "Hello" // it works also with array values: int[] arr = new[] { 1, 2,

Does passing Reference Types using ref save memory?

一个人想着一个人 提交于 2019-12-01 03:59:48
In C#, the parameters to a method can be either reference types or value types. When passing reference types, a copy of the reference is passed. This way, if inside a method we try to reassign the passed reference to another object instance, outside of the method the reassignment is not visible. To make this working, C# has the ref modifier. Passing a reference type with ref actually uses the original reference instead of a copy. (Correct me if I'm wrong). In this case, since we are not creating a copy of the reference, are we saving any memory? If a method is extensively called, does this

Using REF & OUT keywords with Passing by Reference & Passing by Value in C#

ぐ巨炮叔叔 提交于 2019-12-01 00:59:12
问题 Here is what I understand so far: PASS BY VALUE Passing by value means a copy of an argument is passed. Changes to that copy do not change the original. PASS BY REFERENCE Passing by reference means a reference to the original is passed. changes to the reference affect the original. REF Keyword REF tells the compiler that the object is initialized before entering the function. REF means the value is already set, the method can therefore read it and modify it. REF is two ways, both in and out.

Why would ref be used for array parameters in C#?

冷暖自知 提交于 2019-11-30 20:53:02
I read the page Passing Arrays Using ref and out (C# Programming Guide) and was wondering why we would need to define an array parameter as a ref parameter when it is already a reference type. Won't changes in the callee function be reflected in the caller function? Jon Skeet Won't changes in the callee function be reflected in the caller function? Changes to the contents of the array would be reflected in the caller method - but changes to the parameter itself wouldn't be. So for example: public void Foo(int[] x) { // The effect of this line is visible to the caller x[0] = 10; // This line is

Vue - access nested childs using ref

白昼怎懂夜的黑 提交于 2019-11-30 20:35:12
I have vue component which I use inside himself - data can have array with subelements and I use this array to render them in loop, and next level, next level etc. according to nesting level. Now I would like to run child method from parent and then - if statements are ok, also call it to child, next level etc. I use <mycomponent ref="myFirstLevelRefName" (...) ></mycomponent> and then: this.$refs.myFirstLevelRefName to call first-level childs. But what about about child nodes? I use them in view in that way: <mycomponent v-for="(subElement, index) in getSubelements()" ref="???" v-bind:data=

Are ref and out in C# the same a pointers in C++?

有些话、适合烂在心里 提交于 2019-11-30 20:10:53
I just made a Swap routine in C# like this: static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } It does the same thing that this C++ code does: void swap(int *d1, int *d2) { int temp=*d1; *d1=*d2; *d2=temp; } So are the ref and out keywords like pointers for C# without using unsafe code? They're more limited. You can say ++ on a pointer, but not on a ref or out . EDIT Some confusion in the comments, so to be absolutely clear: the point here is to compare with the capabilities of pointers. You can't perform the same operation as ptr++ on a ref / out , i.e. make it address

Returning vs. using a reference parameter

家住魔仙堡 提交于 2019-11-30 08:14:50
This is really bugging me, coming from a C# background. Sometimes, I see functions written like this: int computeResult(); This is what I'm used to. But then I see them written like this: void computeResult(int &result); I find this strange. What benefits does the second method have over the first, if any? There must be something, since I see it all the time. There are two common reasons for such non-const reference parameters: You may need multiple "out" parameters in a function, and using reference parameter(s) allows for this. Your object may be expensive to copy, and so you pass in a

Why would ref be used for array parameters in C#?

不羁的心 提交于 2019-11-30 05:18:42
问题 I read the page Passing Arrays Using ref and out (C# Programming Guide) and was wondering why we would need to define an array parameter as a ref parameter when it is already a reference type. Won't changes in the callee function be reflected in the caller function? 回答1: Won't changes in the callee function be reflected in the caller function? Changes to the contents of the array would be reflected in the caller method - but changes to the parameter itself wouldn't be. So for example: public