标签: ref

修改集合中对象的属性

我有一组对象,并希望修改该集合中对象的属性.如果我有一个对象,我的ChangeStuff方法工作正常,从方法返回时修改对象.(Main的前4行)

然而,当迭代整个集合时,我想我错过了一些东西,因为我的更改值似乎停留在foreach循环的范围内.

我不应该通过ref(或使用out参数)传递对象,因为我没有返回新值.

对于大代码块很抱歉,但它简化了我可以做到并且仍然可以证明我的问题.

    class foobar
    {
        public string string1;
        public string string2;
    }

    static void Main(string[] args)
    {
        /***** THIS WORKS!! *****/
        foobar singleFb = new foobar { string1 = "foo2", string2 = "bar2" };
        ChangeStuff(singleFb);
        Console.WriteLine(singleFb.string1 + ", " + singleFb.string2);

        Console.ReadLine(); //pause to read output

        /***** THIS DOESN'T WORK!! *****/
        List<foobar> myList = new List<foobar> { new foobar {string1 = "foo1", string2 = "bar1"}, new foobar {string1 = "foo2", string2 = "bar2"}, new …
Run Code Online (Sandbox Code Playgroud)

c# linq ref

1
推荐指数
2
解决办法
83
查看次数

在数组上使用'ref'关键字

我理解为什么ref在编写函数来交换两个值时应该使用,但我不知道如何在整个数组上使用该关键字.这听起来很愚蠢,但我已经尝试将关键字粘贴到我可能想到的任何地方(例如在参数之前,变量之前等等)但是我仍然得到以下错误:

错误1非静态字段,方法或属性'Swap.Program.swapRotations(int [])'需要对象引用

这是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = {0, 1, 2, 3, 4, 5, 6, 7};

            swapRotations(A);

            for (int i = 0; i < A.Length; i++)
                Console.WriteLine(A[i]);

            Console.WriteLine("\nPress any key ...");
            Console.ReadKey();
        }

        private void swapRotations(int[] intArray)
        {
            int bone1Rot = intArray[3];
            int bone2Rot = intArray[5];

            // Make the swap.
            int temp = bone1Rot;
            bone1Rot = bone2Rot;
            bone2Rot …
Run Code Online (Sandbox Code Playgroud)

c# arrays ref

1
推荐指数
1
解决办法
268
查看次数

为什么 INotifyPropertyChanged 的​​ SetProperty() 方法需要一个 ref 参数?

考虑到 INotifyPropertyChanged 的​​实现通常如下所示:

    public class Observable : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void SetProperty<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
        {
            if (Equals(storage, value))
            {
                return;
            }

            storage = value;
            OnPropertyChanged(propertyName);
        }

        protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
Run Code Online (Sandbox Code Playgroud)

为什么该SetProperty方法需要 ref 参数?几乎不可能将类字段以外的任何内容传递给该方法,因此无论如何它应该始终是引用类型?

注意:我问这个问题是因为我想将此方法用于通过foreach循环枚举的项目,该方法不适用于ref关键字。

c# ref inotifypropertychanged

1
推荐指数
1
解决办法
452
查看次数

在 useMemo 中 React.createRef()

使用这样的模式是否安全:

const appLoaders = useMemo(() => React.createRef(), [])
Run Code Online (Sandbox Code Playgroud)

问题是我在 useEffect 中使用了这个 ref 并且在依赖数组(exhaustive-deps)中需要它。上面的模式可以解决问题,一切似乎都有效 - 没有备忘录,当我将 ref 放入依赖项数组时,应用程序处于不断重新渲染状态。

我只是想知道在某些情况下是否有一些“陷阱”会让我感到惊讶。

ref memo reactjs react-hooks

1
推荐指数
1
解决办法
508
查看次数

React Forwardref 当前始终为空

因此,我正在尝试为我的组件实现 React Forwardref 示例,因为我想控制内部的滚动条。

这是我所拥有的:

const MessagesFC = React.forwardRef((props, ref) => {       
    return <Flex
        className={"Messages"}
        direction={"Column"}
        ref={ref}>
        {messages}
    </Flex>
});

const scrollDivRef = React.createRef<any>();

React.useEffect(() => {
    scrollDivRef.current?.scrollIntoView({ behavior: "smooth"})
}, [ scrollDivRef, chatRoom.messages ])
Run Code Online (Sandbox Code Playgroud)

在我的 return() 中,该组件是这样添加的:

<MessagesFC 
    ref={scrollDivRef}/>
Run Code Online (Sandbox Code Playgroud)

似乎设置就像示例中一样。

但是,执行此代码时,scrollDivRef.current 始终未定义。

那么,我哪里做错了呢?

我的代码截图:

在此输入图像描述

ref reactjs react-hooks

1
推荐指数
1
解决办法
5006
查看次数

引用数组,哈希等的十六进制数表示什么?

打印对数组,哈希等的引用时,括号中的十六进制数是多少?

perl -e 'print []'
Run Code Online (Sandbox Code Playgroud)

给出如下输出:ARRAY(0x9acb830)

什么是0x9acb830?如果我再次打印相同的参考号,则此数字会更改.

perl ref

0
推荐指数
1
解决办法
1507
查看次数

c#ref for speed

我完全理解.NET中的引用词

既然使用相同的变量,会增加使用ref而不是复制的速度吗?

我发现密码一般是瓶颈.

这是我的代码

protected internal string GetSecurePasswordString(string legalChars, int length)
{
    Random myRandom = new Random();
    string myString = "";
    for (int i = 0; i < length; i++)
    {
        int charPos = myRandom.Next(0, legalChars.Length - 1);
        myString = myString + legalChars[charPos].ToString();
    }
    return myString;
}
Run Code Online (Sandbox Code Playgroud)

在lawchars之前更好地参考?

c# performance ref

0
推荐指数
2
解决办法
1124
查看次数

c#参数为'ref',而参数声明为'value'

public virtual IAsyncResult BeginProcessRequest(RequestContext context, AsyncCallback callback, object state)
{
    return this.process.BeginInvoke(**ref context**, callback, state);
}

public virtual RequestContext EndProcessRequest(IAsyncResult result)
{
    RequestContext context = null;
    this.process.EndInvoke(**ref context**, result);
    return context;
}
Run Code Online (Sandbox Code Playgroud)

上面的两种方法在我的项目中引起了一些警告.我不确定我理解他们.警告是:

参数为'ref',而参数声明为'value'

并且警告的位置是Invoke调用中的第一个参数(上下文).有没有人看到这个有什么问题或者对这个问题有一些建议?

那些双星号是警告的原因.我在编辑器上点击"粗体",它就这样做了,所以我就去了.星号不在我的代码中.

c# resharper performance ref

0
推荐指数
2
解决办法
1万
查看次数

f#自定义链接列表和引用

我是这门语言的新手.为了尝试和理解参考我已经尝试实施一个简单的定向列表新生大学计算机科学的方式.

type item = { 
    value:float 
    next:ref<item>
}

type list() = 
    let head:ref<item> = null // tried instantiation so many different ways and it likes none of em

    let sum i = 
        if i == null then
            0
        else 
            i.value + sum i.next // constructor not defined?
Run Code Online (Sandbox Code Playgroud)

请告诉我为什么我不好

recursion f# list ref

0
推荐指数
1
解决办法
222
查看次数

OCaml,refs和树的意外行为

我试图为树的每个元素分配一个数字.我认为使用refs会使任务更容易,但我遇到了一个奇怪的行为:分配的数字不是唯一的,没有明确的模式出现.我设法修复了错误(添加了let unboxed = !second_ref in行),但我不明白发生了什么.

输出控制台中的第一个树只是确保print_tree函数输出它应该的内容.

但是,第二次打印所需的输出应与第三次打印完全相同.我错过了什么?

type ('a, 'b) tree =
  | Node of 'a * ('a, 'b) tree * ('a, 'b) tree
  | Leaf of 'b

let print_tree tree string_of_node string_of_leaf  =
  let rec print indent tree =
    match tree with
    | Leaf (l) -> print_string (indent^" -> "^string_of_leaf(l)^"\n")
    | Node (n, left, right) ->
      Printf.printf "%s-----------\n" indent;
      print (indent ^ "|         ") left;
      Printf.printf "%s%s\n" indent (string_of_node(n));
      print (indent ^ "| …
Run Code Online (Sandbox Code Playgroud)

debugging tree ocaml ref

0
推荐指数
1
解决办法
61
查看次数