mojtaba sabet mojtaba sabet - 3 months ago 6
ASP.NET (C#) Question

What is the meaning of base["Description"] and Create(username)?

Can any one describe for me the role of

base[""]
and
Create()
in this code?

public class UserProfile : ProfileBase
{
public static UserProfile GetUserProfile(string username)
{
return Create(username) as UserProfile;
}

[SettingsAllowAnonymous(false)]
public string Description
{
get { return base["Description"] as string; }
set { base["Description"] = value; }
}

}

Answer

The base["Description"] syntax is what is known as an Indexer in .NET. You can define indexers on your own classes with a property declaration using the this keyword like:

public class MyClass
{
    //indexer (could use int or anything else that your underlying collection supports)
    public string this[string index]
    {
        get
        {
             //retrieve from internal cache/collection/etc based on index
        }
        set
        { 
             //set internal cache/collection/etc based on index and value
        }
    }
}

And then use it like

var myclass = new MyClass();
var value = myclass["index"];
myclass["another"] = "new value";

In your example, ProfileBase has defined an Indexer, and UserProfile is accessing it via the base keyword, since ProfileBase is the base class of UserProfile.

Comments