在同一个类中定义和调用自动实现的属性

时间:2017-04-19 21:10:43

标签: c# oop

好的,我真的很惭愧但是......已经宣布了

public class HomeController : BaseController
{
    public bool IsAuthenticated
    {
        get
        {
            return this.HttpContext.User != null &&
                   this.HttpContext.User.Identity != null &&
                   this.HttpContext.User.Identity.IsAuthenticated;
        }
    }

为什么我不能在同一个类的方法中执行此操作?

    public ActionResult Index()
    {
        if(IsAuthenticated)
        {...do something...}
    }

为:

The name 'IsAuthenticated' does not exist in the current context

实例化类以创建对其“IsAuthenticated”自动实现属性的引用是没有意义的,因为我调用ActionResult Index方法时触发了类默认构造函数(即使它不需要显式声明)。 / p>

...困惑

0 个答案:

没有答案