我想从 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) 这有效:
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关键字会破坏参数的逆转?
令我沮丧的是,以下代码不会编译.
但是,如果我删除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)
有谁能解释一下?我猜这与ref对派生类非常严格有关.
有什么办法可以传递string类型的对象foo(ref
object varname)吗?
我希望能够替换参数的对象引用,而不必使用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) 项目中的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#语言.
在我的代码中,我有一个从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)
编译错误是:
任何人都可以解释为什么这是不允许的,或者编译器无法做到这一点?我有一个使用泛型的解决方法,所以我只想了解为什么不允许这样做.
谢谢.
ref cell是否像指针那样引用堆上的数据,需要明确删除?我在网上看到的所有例子都没有明确的删除调用.
我想我前段时间在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%肯定.
我很确定应该有关于此的文档,但我没有找到它.
将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)