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

后端 未结 5 1315
情深已故
情深已故 2021-01-04 12:46

In C# when I want to call a static method of a class from another static method of that class, is there a generic prefix that I can use such as PHP\'s

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 13:46

    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.

    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;
        }
    }
    

提交回复
热议问题