简体   繁体   English

C# 中 .NET 远程处理中实例的使用

[英]Usage of instance in .NET remoting in C#

I have a problem regarding the usage of instances in C#.我在使用 C# 中的实例时遇到问题。 Can you please explain me the difference between the following 2 code lines:您能否解释一下以下两行代码之间的区别:

public static DatabaseClass Instance { get; } = new DatabaseClass();

DatabaseClass _database = DatabaseClass.Instance;

The biggest difference is that this is declaration最大的不同是这是声明

public static DatabaseClass Instance { get; } = new DatabaseClass();

and this is usage这是用法

DatabaseClass _database = DatabaseClass.Instance;

You took 2 lines of code out of context.您将 2 行代码断章取义。 The true context is the classes that have these lines in. One class is most likely a Singleton真正的上下文是包含这些行的类。一个 class 很可能是Singleton

public class DatabaseClass 
{
    private DatabaseClass() {} // hide constructor

    public static DatabaseClass Instance { get; } = new DatabaseClass();
}

And another class has some method that calls the class above另一个 class 有一些方法可以调用上面的 class

public class SomeClass
{
    public static void DoSomething()
    {
        DatabaseClass _database = DatabaseClass.Instance;
        _database.PerformSomeOperation()
    }
}

By simply looking at the declaration, it is a common way to deal with singletons.通过简单地查看声明,这是处理单例的常用方法。

For your question the bottom line is that对于你的问题,底线是

line#1 is a declaration of the static property with default read-only value第 1 行是 static 属性的声明,具有默认只读值

line#2 is usage of that property第 2 行是该属性的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM