C# 存储对对象的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13293073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store a reference to an object
提问by KingTravisG
A bit of a weird question but I was wondering anyone could help...
一个有点奇怪的问题,但我想知道任何人都可以帮忙......
In C++, I could do something like this
在 C++ 中,我可以做这样的事情
class MyOtherClass
{
private:
MyLogger* logger;
public:
MyOtherClass (MyLogger* logger)
: logger (logger)
{}
};
class MyClass
{
private:
MyLogger* logger;
public:
MyClass (MyLogger* logger)
: logger (logger)
{}
};
int main (int c, char** args)
{
MyLogger* logger = new MyLogger ();
/* Code to set up logger */
MyOtherClass* myOtherClass = new MyOtherClass (logger);
MyClass* myClass = new MyClass (logger);
}
So that each of the other objects (myOtherClass and myClass) would contain a pointer to logger, so they would be calling the same logger class. However, how would I achieve the same thing in C#? Is there a way to store a reference or pointer to a global object - I'm guessing that in C# if I do something like
这样每个其他对象(myOtherClass 和 myClass)都将包含一个指向 logger 的指针,因此它们将调用相同的 logger 类。但是,我将如何在 C# 中实现相同的目标?有没有办法存储指向全局对象的引用或指针 - 我猜在 C# 中,如果我做类似的事情
public class MyClass
{
private MyLogger logger = null;
public MyClass (MyLogger _logger)
{
logger = _logger;
}
};
that its actually assigning the class variable logger to a copy of _logger? Or am I'm mixing things up :S
它实际上将类变量 logger 分配给了 _logger 的副本?还是我把事情搞混了:S
Any help is very much appreciated, and thank you in advance!
非常感谢任何帮助,并在此先感谢您!
采纳答案by Tejs
It's actually a lot simpler in C#.
它实际上在 C# 中要简单得多。
Basically, you can do this:
基本上,你可以这样做:
MyLogger logger = new MyLogger();
MyOtherClass myOtherClass = new MyOtherClass(logger);
MyClass myClass = new MyClass(logger);
In C#, the classes are basically kept around as references (really just pointers under the hood). In this snippet, you are passing the referenceto loggerto the constructors of both objects. That reference is the same, so each instance has the same MyLoggerinstance.
在 C# 中,类基本上作为引用保留(实际上只是引擎盖下的指针)。在此代码段中,您将引用传递logger给两个对象的构造函数。该引用是相同的,因此每个实例都有相同的MyLogger实例。
In this particular instance, you pretty much just need to remove the pointer syntax =D
在这个特定的例子中,你几乎只需要删除指针语法 =D
回答by Steve B
If the type is a reference type (which is the case for classes), then you will copy the reference, not the object itself.
如果类型是引用类型(类就是这种情况),那么您将复制引用,而不是对象本身。
In opposition to reference type, you have value types. Values types are basically basic types : int, double, etc,
与引用类型相反,您有值类型。值类型基本上都是基本类型:int,double,等,
In your case, that means that you will work with the same objects, whether you access it from the class, or from the outer calling method. It's because you are targeting the referenced object.
在您的情况下,这意味着您将使用相同的对象,无论您是从类访问它,还是从外部调用方法访问它。这是因为您的目标是引用的对象。
回答by AakashM
You're mixing things up. In C#, assignment statements such as
你把事情搞混了。在 C# 中,赋值语句如
logger = _logger;
copy references, not objects. After this statement executes, there is still (at most) only one MyLogger- it's now referred toby two object variables.
复制引用,而不是对象。这条语句执行后,仍然(最多)只有一个MyLogger——它现在被两个对象变量引用。

