ref

Delegate with ref parameter

笑着哭i 提交于 2019-12-23 07:10:08
问题 Is there any way to maintain the same functionality in the code below, but without having to create the delegate? I'm interfacing with a 3rd-party API that contains a number of various DeleteSomethingX(ref IntPtr ptr) methods and I'm trying to centralize the code for the IntPtr.Zero check. private void delegate CleanupDelegate(ref IntPtr ptr); ... private void Cleanup(ref IntPtr ptr, CleanupDelegate cleanup) { if (ptr != IntPtr.Zero) { cleanup(ref ptr); } } 回答1: If you mean without declaring

Why can readonly fields be modified through ref parameters?

梦想与她 提交于 2019-12-23 07:04:33
问题 Consider: class Foo { private readonly string _value; public Foo() { Bar(ref _value); } private void Bar(ref string value) { value = "hello world"; } public string Value { get { return _value; } } } // ... var foo = new Foo(); Console.WriteLine(foo.Value); // "hello world" How does this even compile, nonetheless work? I should not be able to assign a the different value to _value field outside of the constructor, as it's marked with readonly . However, pass it by ref to a method, and it can

Why can readonly fields be modified through ref parameters?

青春壹個敷衍的年華 提交于 2019-12-23 07:04:22
问题 Consider: class Foo { private readonly string _value; public Foo() { Bar(ref _value); } private void Bar(ref string value) { value = "hello world"; } public string Value { get { return _value; } } } // ... var foo = new Foo(); Console.WriteLine(foo.Value); // "hello world" How does this even compile, nonetheless work? I should not be able to assign a the different value to _value field outside of the constructor, as it's marked with readonly . However, pass it by ref to a method, and it can

Ref Abuse: Worth Cleaning Up?

醉酒当歌 提交于 2019-12-22 03:58:22
问题 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,

what is the alternative for ReactDOM.findDOMNode() as it is deprecated now?

☆樱花仙子☆ 提交于 2019-12-21 12:34:07
问题 I have an old code which is using findDOMNode(). here is my code, where someComponent1 and Expand is already imported Here I have some doubt the code I have written with findDOMNode() is working perfectly fine but as it is deprecated now I want to remove it. I have gone through many document and found to use portals or refs instead of this. I have a understanding that if I am using ref then the variable get bind to that also has an access to the DOM element, but I guess I am wrong as it is

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

百般思念 提交于 2019-12-21 10:18:07
问题 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

How does the ref keyword work (in terms of memory)

吃可爱长大的小学妹 提交于 2019-12-21 03:20:33
问题 C# has a ref keyword. Using ref you can pass an int to a method by reference. What goes on the stack frame when you call a method that accepts an int by reference? public void SampleMethod(ref int i) { } 回答1: Passing a local variable as a reference At low level, the referenced local int variable will be put on the stack (most of the time integers get stored in registers), and a pointer to the stack will be passed to the invoked function (the pointer itself is most likely to be passed in a

How can I assign a new ref to every iteration inside a map function?

本小妞迷上赌 提交于 2019-12-20 05:34:09
问题 I'm not sure how to ask this question, because I'm still unable to accurately frame the problem. I've created a useHover function. Below, you'll see that I am mapping over data and rendering a bunch of photos. However, the useHover only works on the first iteration. I suspect that it's because of my ref. How does this work? Should I creating a new ref inside of each iteration -- or is that erroneous thinking..? How can I do this? Here's my useHover function. const useHover = () => { const ref

In React do ref's reference the virtual DOM, or the actual DOM?

蹲街弑〆低调 提交于 2019-12-20 04:38:32
问题 I'm assuming the virtual DOM, and that React takes care of it with diff'ing. But I had a recruiter say that ref's affect the actual DOM, I can't see how this can be. I assume that they were just mistaken. 回答1: Refs should reference the actual DOM. One usage of Refs is integrating with third-party DOM libraries, so you can directly modify the DOM using Refs. If Refs reference the virtual DOM, I don't think the demand can be meet. You modify a virtual DOM, but you can't make sure the

Which types pass by reference and which by value?

风格不统一 提交于 2019-12-20 03:30:36
问题 If I pass an int it will be passed by value. void something(int i) If I pass some type of array it will be passed by reference. void something(int i[]) OR void something(int *i); Types like int will be passed by value, arrays will be passed as reference. The first question is, are there any more types that will be passed by reference? The second question is, what is the difference between the first pass of the array (int i[]) and the second one (int *i) ? EDIT: I will clarify my question.