为什么C#不传递财产作为参考提供内部帮手? [英] why C# does not provide internal helper for passing property as reference?

查看:112
本文介绍了为什么C#不传递财产作为参考提供内部帮手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到你看了整个帖子,请不要回答的问题!谢谢你。

Please do not answer to the question until you read entire post! Thank you.

对于所有在C#中现有的(如lambda表达式,或自动属性)的助手是很奇怪的,我说我不能被引用传递属性。比方说,我想做到这一点:

With all helpers existing in C# (like lambdas, or automatic properties) it is very odd for me that I cannot pass property by a reference. Let's say I would like to do that:

foo(ref my_class.prop);



我得到的错误,所以我写来代替:

I get error so I write instead:

{
   var tmp = my_class.prop;
   foo(tmp);
   my_class.prop = tmp;
}

和现在的作品。但是,请注意两件事情:

And now it works. But please notice two things:


  1. 这是通用模板,我没有把任何地方的类型,只有无功,所以它适用于财产所有类型和号码,我必须要通过

  1. it is general template, I didn't put anywhere type, only "var", so it applies for all types and number of properties I have to pass

我必须这样做一遍又一遍,没有好处 - 它是机械的工作

I have to do it over and over again, with no benefit -- it is mechanical work

存在的问题居然杀死这类有用的功能交换。互换通常是3线长,但由于它需要2引用,称它需要5行。当然,这是胡说八道,我只是写我每次想叫它交换的手。但是,这显示了C#防止可重用的代码,坏

The existing problem actually kills such useful functions as Swap. Swap is normally 3 lines long, but since it takes 2 references, calling it takes 5 lines. Of course it is nonsense and I simply write "swap" by hand each time I would like to call it. But this shows C# prevents reusable code, bad.

那么 - 有什么不好的可能发生,如果编译器会自动创建临时变量(如我做手工),调用函数,并分配值回属性?这是在任何危险吗?我没有看到它,所以我很好奇你怎么想,为什么这个问题的设计看起来像它现在看起来。

So -- what bad could happen if compiler automatically create temporary variables (as I do by hand), call the function, and assign the values back to properties? Is this any danger in it? I don't see it so I am curious what do you think why the design of this issue looks like it looks now.

干杯,

修改作为280Z28放弃对性能我还是觉得用临时变量包装性能将是有益的跳动自动ref包装的理念很好的例子。也许是这样的:

EDIT As 280Z28 gave great examples for beating idea of automatically wrapping ref for properties I still think wrapping properties with temporary variables would be useful. Maybe something like this:

Swap(inout my_class.prop1,inout my_class.prop2);



否则,对于C#没有真正的交换: - (

Otherwise no real Swap for C# :-(

推荐答案

有很多假设,你可以对意义和 REF的行为参数,例如:

There are a lot of assumptions you can make about the meaning and behavior of a ref parameter. For example,

案例1:

int x;
Interlocked.Increment(ref x);

如果您可以通过文献传递一个属性,以这种方法,调用会相同的,但它会的完全的打败方法的语义

If you could pass a property by ref to this method, the call would be the same but it would completely defeat the semantics of the method.

案例2:

void WaitForCompletion(ref bool trigger)
{
    while (!trigger)
        Thread.Sleep(1000);
}

摘要:由-ref参数传递一个内存位置函数的地址的一种实现创建。以按引用传递属性将是语义上等同于价值,这恰恰是你不允许的行为时,你的参数 REF 之一。

Summary: A by-ref parameter passes the address of a memory location to the function. An implementation creating a temporary variable in order to "pass a property by reference" would be semantically equivalent to passing by value, which is precisely the behavior that you're disallowing when you make the parameter a ref one.

这篇关于为什么C#不传递财产作为参考提供内部帮手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆