相当于 PHP 的“self::”的 C# 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/857571/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:27:00  来源:igfitidea点击:

What is the C# equivalent to PHP's "self::"?

c#oop

提问by Edward Tanguay

In C# when I want to call a static method of a class from another static method of that class, is there a generic prefixthat I can use such as PHP's self::instead of the class name?

在 C# 中,当我想从该类的另一个静态方法调用一个类的静态方法时,是否有我可以使用的通用前缀,例如 PHP,self::而不是类名?

So in the below example, instead of saying Customer.DatabaseConnectionExists(), how can I say something like Self.DatabaseConnectionExists()so e.g. later if I change the name of the class I don't have to go change all the prefixes?

所以在下面的例子中,不是说Customer.DatabaseConnectionExists(),我怎么能说这样的话,例如,Self.DatabaseConnectionExists()如果我改变了类的名称,我不必去改变所有的前缀?

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Customer GetCurrentCustomer()
    {
        if (Customer.DatabaseConnectionExists())
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
        else
        {
            throw new Exception("Database connection does not exist.");
        }
    }

    public static bool DatabaseConnectionExists()
    {
        return true;
    }
}

采纳答案by Noldorin

There's no real equivalent - you have to either specify the class name, i.e.

没有真正的等价物 - 您必须指定类名,即

Customer.DatabaseConnectionExists()

or miss out the qualifier altogether, i.e.

或完全错过预选赛,即

DatabaseConnectionExists()

The latter style of calling is advisable since it's simpler and doesn't lose any meaning. Also, it's more inline with method calling in instances (i.e. calling by InstanceMethod()and notthis.InstanceMethod(), which is overly verbose).

后一种调用方式是可取的,因为它更简单并且不会失去任何意义。此外,它更符合实例中的方法调用(即调用 byInstanceMethod()notthis.InstanceMethod(),这过于冗长)。

回答by Fabio Vinicius Binder

No, there isn′t. But with the refactor tools, changing a name of a class should not worry you too much.

不,没有。但是使用重构工具,更改类的名称不应该让您太担心。

回答by Kirschstein

If you're calling the method from inside the class, you don't need to specify anything like ::Self, just the method name will do.

如果您从类内部调用该方法,则不需要指定诸如 ::Self 之类的任何内容,只需指定方法名称即可。

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Customer GetCurrentCustomer()
    {
        if (DatabaseConnectionExists())
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
        else
        {
            throw new Exception("Database connection does not exist.");
        }
    }

    public static bool DatabaseConnectionExists()
    {
        return true;
    }
}

回答by Dario

Just leave it out. DatabaseConnectionExistsis defined inside the class.

把它放在外面。DatabaseConnectionExists在类内部定义。

回答by Alex Jenter

Just call it without any prefix.

无需任何前缀即可调用它。