标签: ref

如何从 lambda 内部修改 ref 变量

我想从 lambda 内部分配给 ref 变量。这是我所拥有的简化的 MRE:

public class Foo
{
    public int Value { get; private set; }
    public Foo(int value) => this.Value = value;
}

public class Test
{
    Foo F1 = new Foo(1);
    Foo F2 = new Foo(2);

    // library method, cannot change
    void LoadAsync(int value, Action<Foo> onSuccess)
    {
        // this happens async on a future frame
        Foo f = new Foo(value);
        onSuccess(f);
    }

    // my method, can change parameters and body, cannot change return type
    void LoadAndAssignAsync(int …
Run Code Online (Sandbox Code Playgroud)

c# lambda ref

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

为什么ref参数不是逆变的?

这有效:

EndPoint endPoint = new IPEndPoint(_address, _port);
_socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint);
Run Code Online (Sandbox Code Playgroud)

但这不是:

IPEndPoint endPoint = new IPEndPoint(_address, _port);
_socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint);
Run Code Online (Sandbox Code Playgroud)

(注意endPoint的类型)

这似乎很奇怪.为什么ref关键字会破坏参数的逆转?

c# ref contravariance

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

Ocaml自我引用

我创建了类型t =测试int*t ref

如何创建t类型的任何对象?

recursion ocaml ref

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

通过'ref'传递 - c#

令我沮丧的是,以下代码不会编译.

但是,如果我删除ref关键字,它将编译.

class xyz
{
    static void foo(ref object aaa)
    {
    }

    static void bar()
    {
        string bbb="";
        foo(ref bbb);
        //foo(ref (object)bbb); also doesnt work
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 有谁能解释一下?我猜这与ref对派生类非常严格有关.

  2. 有什么办法可以传递string类型的对象foo(ref object varname)吗?

c# ref

4
推荐指数
2
解决办法
330
查看次数

不使用ref关键字替换参数的ref(使用IL)

我希望能够替换参数的对象引用,而不必使用ref关键字.

我避免使用ref的原因是保留了查找Add(T item)方法的集合初始化程序调用,我需要让集合类用它的接口的不同实现替换引用.

我尝试了几种不同的方法来做到这一点.首先,我尝试使用未记录的关键字__makeref,__refvalue并且__reftype.

其次,我尝试DynamicMethod用一些IL 来创建一个IL,它试图模仿我从一个带有ref参数的反汇编类似调用中观察到的东西.

以下是一些演示代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Reflection.Emit;
using System.Reflection;
interface IRecord
{
    string Name { get;}
}
class ImpA : IRecord
{
    public string Name { get { return "Implementation A"; } }
}
class ImpB : IRecord
{
    public string Name { get { return "Implementation B"; } }
}
class RecordList<T> : IEnumerable<T>
{
    //// Standard Add method (of course …
Run Code Online (Sandbox Code Playgroud)

c# il cil ref dynamicmethod

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

将未初始化的参数传递给方法时的ref关键字

项目中的BindableBase抽象基类WinRT定义如下:

[Windows.Foundation.Metadata.WebHostHidden]
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

没关系.

现在我看到许多文章试图实现这个类,做这样的事情:

private  int _timeEstimate;
        public int TimeEstimate
        {
            get { return this._timeEstimate; }
            set …
Run Code Online (Sandbox Code Playgroud)

c# ref mvvm windows-runtime

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

C#为什么我不能通过ref传递"base"接口?

我想知道为什么这是一个编译错误,以便更好地理解C#语言.

在我的代码中,我有一个从IDisposable派生的接口(IMyInterface).我有另一种方法,它采用'ref IDisposable'类型的参数.但我不能将IMyInterface类型的成员var传递给该方法.这是我的示例代码如下:

using System;

namespace CompileErrorBaseInterface
{
    public interface IMyInterface : IDisposable { }

    class Program
    {
        private IMyInterface _myInterfaceObj;

        static void Main(string[] args) { }

        public void ExampleMethod()
        {
            MyMethodBaseInterface(ref _myInterfaceObj); //compile error
            MyMethodDerivedInterface(ref _myInterfaceObj); //no compile error
        }

        private void MyMethodBaseInterface(ref IDisposable foo) { }

        private void MyMethodDerivedInterface(ref IMyInterface foo) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译错误是:

  • 参数1:无法从'ref CompileErrorBaseInterface.IMyInterface'转换为'ref System.IDisposable'最佳重载方法匹配
  • 'CompileErrorBaseInterface.Program.MyMethodBaseInterface(ref System.IDisposable)'有一些无效的参数

任何人都可以解释为什么这是不允许的,或者编译器无法做到这一点?我有一个使用泛型的解决方法,所以我只想了解为什么不允许这样做.

谢谢.

c# oop interface ref

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

F# - 需要明确删除ref cell吗?

ref cell是否像指针那样引用堆上的数据,需要明确删除?我在网上看到的所有例子都没有明确的删除调用.

heap f# pointers ref delete-operator

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

什么是定义网站中的ref struct

我想我前段时间在GitHub中听过"ref like struct"这个词.

现在我掌握了最新的C#版本(7.3),我终于可以自己测试了.所以这似乎是一个有效的代码:

public ref struct MyStruct
{
    int x;
}
Run Code Online (Sandbox Code Playgroud)

我知道什么是ref本地和ref返回,因为有关于它的文档.但是我找不到关于ref struct的文档.


引用结构不能用于自动属性或字段.它们也不能被投射到对象上.这些都是实证研究结果.

有了新的c#最近给我的"Span"背景,我猜测ref struct是一个只有堆栈的结构.这是一个永远不会堆的结构.但我不是100%肯定.

我很确定应该有关于此的文档,但我没有找到它.

c# struct ref c#-7.0

4
推荐指数
3
解决办法
3984
查看次数

在垃圾收集时,NodeJS中的内容会损坏ref-struct

ref-struct实例嵌套在另一个实例中,嵌套对象的其中一个属性在手动垃圾回收时会损坏.

请参阅此最小代码复制:https://github.com/hunterlester/minimum-ref-struct-corruption

请注意日志输出的第3行,其值name未损坏:

Running garbage collection...
authGranted object afte gc:  { name: '?_n9a\u0002', 'ref.buffer': <Buffer@0x00000261396F3910 18 86 6c 39 61 02 00 00> }
Unnested access container entry after gc:  { name: 'apps/net.maidsafe.examples.mailtutorial', 'ref.buffer': <Buffer@0x00000261396F3B10 60 68 6e 39 61 02 00 00> }
Globally assigned values after gc:  apps/net.maidsafe.examples.mailtutorial  _publicNames
Run Code Online (Sandbox Code Playgroud)

garbage-collection ref node.js ref-struct

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