C#值类型成员(通过引用) [英] C# value type member by reference

查看:86
本文介绍了C#值类型成员(通过引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello CodeProject专家,

我需要一些C#基础知识的帮助...查看此C代码,请告诉我如何在C#中以简单文明的方式执行此操作:

Hello CodeProject Gurus,

I need help with some C# fundamentals... Look at this C code and please tell me how to do this in a simple and civilized way in C#:

class A
{
public:
    int* p;
    A(int* pv) : p(pv) {}
    void DoPlusPlus() { (*p)++; }
};

int i = 5;
A a(&i);
a.DoPlusPlus();



我有很多这样的课程,我只是碰壁.我需要做的就是用某个标量初始化一个类的实例,并让该实例稍后能够修改此标量的值.



I have a whole bunch of classes like this where I am just hitting the wall. All I need to do is to initialize an instance of a class with some scalar and have this instance to be able to modify the value of this scalar later on.

推荐答案

我认为对此的答案是:您不应该那样做.如果C#程序员看到一个int变量,除非他将其作为方法的引用传递给他,否则他不会期望它会更改.

简单的技术解决方案是
I think the answer to this one is: you shouldn''t do it that way. If a C# programmer sees an int variable he won''t expect it to change unless he passes it as ref to a method.

The simple technical solution is
class WrappedInt {
 public int i; 

 public WrappedInt(int i) { this.i = i; }
}

class A {
 private WrappedInt p;
 public A(WrappedInt pv) { p = pv; }
 public void DoPlusPlus() { p.i++; }
}

WrappedInt i = new WrappedInt(5);
A a = new A(i);
a.DoPlusPlus();



您可以将其扩展到一个通用拳击类,该类可以相对容易地隐式地转换为内部类型和从内部类型隐式转换.不过,我不会显示*,因为我认为这是走错了路.

但这似乎是一件奇怪的事情.如果您的A类修改了应用程序的状态,则它应该以更明确和明显的方式进行修改.给A全局状态(或数据模型或任何我代表的状态)的实例,并使其直接更新:



You can extend this to a generic boxing class that is implicitly castable to and from its inner type relatively easily. I''m not going to show that*, though, because I think this is the wrong road to go down.

But this seems like a strange thing to be doing. If your A class modifies the state of the application, it should do so in a more explicit and obvious way. Give A an instance of the global state (or data model or whatever i represents a piece of) and have it update that directly:

class State {
 public int i; 
 public int otherStateVariable;
 // ... etc
}

class A {
 State state;
 public A(State state) { this.state = state; }
 public void DoPlusPlus { state.i++; }
}



在实际情况下,状态或数据模型将公开通知其他层的属性,依此类推.

当然,到目前为止,最好的办法是编写A时没有外部依赖关系,并且绝对没有外部可写依赖关系(那些确实使应用程序难以维护),但是我猜你在想什么已经想到要这样做.

*:我将其发布为提示/技巧 [ ^ ].



In a real scenario the state or data model would expose properties that would notify other layers and so on.

Of course the best by far is if A can be written with no external dependencies and definitely with no external writable dependencies (those are what really make an application hard to maintain), but I''m guessing you already thought of trying to do that.

*: I posted it as a Tip/Trick[^] instead.


嗯.我想这只是一个示例,而不是真实的代码,因为您根本不会创建一个用于增加整数的类.

C#也没有整数指针的概念(撇开不安全的代码),因此,如果尝试这样做,您将遇到装箱"的问题,也就是说,当您创建引用时,它将实际上复制值和引用而不是要引用的内容,这对C ++类型非常容易混淆.

一种方法可能是将标量包装在某种类中以使其成为引用类型.

我需要吃午饭.您能否再解释一下为什么需要这种构造-这种方法确实不适用于.NET.
Hmm. I guess this is an example rather than real code because simply you wouldn''t create a class to increment an integer.

C# doesn''t have the concept of integer pointers either (unsafe code aside) so you''re going to run into the problem of ''boxing'' if you attempt this, that is when you create a reference, it will actually copy the value and reference that rather than the thing you want to reference - very confusing to C++ types.

One approach might be to wrap your scalar in some sort of class to make it a reference type.

I need to get some lunch. Can you explain a little more why you need these sort of constructs - this approach really doesn''t lend itself well to .NET.


这篇关于C#值类型成员(通过引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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