I have the following class written by someone else that I’m trying to understand (I omitted the code that is not necessary for this example):
public abstract class UserControl : System.Web.UI.UserControl
{
...
public virtual bool IsAadmin(TheUser theUser)
{
if (Page is Page)
return ((Page)Page).IsAadmin(theUser);
return false;
}
...
Edit: (note: in stackoverflow the first Page in the if statement is highlighted blue but in visual studio it isn’t)
In the if statement intellisense shows that the first Page is of type System.Web.UI.Page Control.Page and the second Page is from Something.Products.Web.Page. When I debug through the code, it doesn’t seem to go into this if statement, so I’m wondering what this code is trying to do? But more importantly where is that first Page in the if statement is coming from (initialized)? I’m using Resharper and it suggest replacing the if statement with
var page = Page as Page;
if (page != null)
This change shows that page is null when debugging through it.
The “first page” is actually a
Pageproperty ofUserControlclass your abstractUserControlinherits from. It’s of typeSystem.Web.UI.Pageand, as per documentation, returnsThe
iskeyword evaluates to true whenIt seems that when you were debugging your code, the examined
UserControlwas not contained in a page of typeSomething.Products.Web.Page.Regarding the Resharper suggestion: in your original code, you are effectively casting the
Pageproperty twice – first, when usingiskeyword and second when casting it explicitly. With the code generated by Resharper, the cast only occurs once. When theifstatement evaluates to true, you can be sure that thepagevariable holds a reference to theSomething.Products.Web.Pageand do not have to cast it again.