ref

Vue - access nested childs using ref

。_饼干妹妹 提交于 2019-11-30 04:05:51
问题 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

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

笑着哭i 提交于 2019-11-30 03:34:54
问题 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? 回答1: 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

What is the difference between 2 methods with ref object par and without?

不问归期 提交于 2019-11-29 14:06:37
I wonder what the difference is between the following methods with regards to how the object parameter is referenced: public void DoSomething(object parameter){} and public void DoSomething(ref object parameter){} Should I use ref object parameter in cases where I want to change the reference to the object not override the object in the same reference? public void DoSomething(object parameter) { parameter = new Object(); // original object from the callee would be unaffected. } public void DoSomething(ref object parameter) { parameter = new Object(); // original object would be a new object }

Returning vs. using a reference parameter

久未见 提交于 2019-11-29 11:12:15
问题 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. 回答1: There are two common reasons for such non-const reference parameters: You may need multiple "out" parameters in a function, and

Reference equality of value types

不打扰是莪最后的温柔 提交于 2019-11-29 10:47:59
问题 I have made some ref keyword tests and there is one think I can't understand: static void Test(ref int a, ref int b) { Console.WriteLine(Int32.ReferenceEquals(a,b)); } static void Main(string[] args) { int a = 4; Test(ref a, ref a); Console.ReadLine(); } Why does this code display False ? I know that int is a value type but here it should pass references to the same object. 回答1: Why does this code display False ? Because int a and int b are being boxed when you call object.ReferenceEquals.

How to save a ref variable for later use?

早过忘川 提交于 2019-11-29 09:16:54
So this works.. public MyClass(ref Apple apple) { apple = new Apple("Macintosh"); // Works fine } But is it possible to do something like this? private Apple myApple; public MyClass(ref Apple apple) { myApple = apple; } public void ModifyApple() { myApple = new Apple("Macintosh"); // does not change the input variable like the first example did } When the ref variable is copied to the member variable myApple it appears to lose it's 'ref-ness' and re-assigning it no longer changes the input variable. Is there a way around this? Not really, no. By the time your code is next invoked, the original

F# member constraints + ^a byref parameters

拟墨画扇 提交于 2019-11-29 06:55:30
After some playing around F# member constraints feature and writing function like this: let inline parse< ^a when ^a : (static member Parse: string -> ^a) > s = (^a: (static member Parse: string -> ^a) s) That works perfectly fine: let xs = [ "123"; "456"; "999" ] |> List.map parse<int> I'm trying to write other func tryParse , that uses static method TryParse and wraps the parse result into 'a option type for better support in F#. Something like this doesn't compiles: let inline tryParse s = let mutable x = Unchecked.defaultof< ^a> if (^a: (static member TryParse: string * ^a byref -> bool)

“ref” keyword and reference types [duplicate]

老子叫甜甜 提交于 2019-11-28 13:40:00
This question already has an answer here: What is the use of “ref” for reference-type variables in C#? 10 answers someone in my team stumbled upon a peculiar use of the ref keyword on a reference type class A { /* ... */ } class B { public void DoSomething(ref A myObject) { // ... } } Is there any reason someone sane would do such a thing? I can't find a use for this in C# Let class A { public string Blah { get; set; } } void Do (ref A a) { a = new A { Blah = "Bar" }; } then A a = new A { Blah = "Foo" }; Console.WriteLine(a.Blah); // Foo Do (ref a); Console.WriteLine(a.Blah); // Bar But if

C# Cannot use ref or out parameter inside an anonymous method body

有些话、适合烂在心里 提交于 2019-11-28 08:53:09
I'm trying to create a function that can create an Action that increments whatever integer is passed in. However my first attempt is giving me an error "cannot use ref or out parameter inside an anonymous method body". public static class IntEx { public static Action CreateIncrementer(ref int reference) { return () => { reference += 1; }; } } I understand why the compiler doesn't like this, but nonetheless I'd like to have a graceful way to provide a nice incrementer factory that can point to any integer. The only way I'm seeing to do this is something like the following: public static class

What is the difference between 2 methods with ref object par and without?

折月煮酒 提交于 2019-11-28 08:07:41
问题 I wonder what the difference is between the following methods with regards to how the object parameter is referenced: public void DoSomething(object parameter){} and public void DoSomething(ref object parameter){} Should I use ref object parameter in cases where I want to change the reference to the object not override the object in the same reference? 回答1: public void DoSomething(object parameter) { parameter = new Object(); // original object from the callee would be unaffected. } public