简体   繁体   中英

what does square bracket means [] in a property?

I was digging into MVC 4 WebGrid's code, and i see it's used as this

    grid.Column("Id", format: (item) => item.GetSelectLink(item.Id)),

I wasn't sure what the "item" parameter is, so I looked into the source code, and it seems like it's a "WebGridRow" class. The issue for me is, above code is looking for an ".Id" property, but it doesn't exist in the class. The only property that is anywhere close to it looks like the follwing

public object this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
                }
                object value = null;
                if (!TryGetMember(name, out value))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      HelpersResources.WebGrid_ColumnNotFound, name));
                }
                return value;
            }
        }

I have never seen a property defined like "this[string name]", what does it mean? dynamic property?

I tried to search on the web, but not sure what to search for, I couldn't come up with any results / explanation for it.

Please help. Thanks

I think there's two consents here you don't understand (or I simply don't understand your question).

this[..] properties are index-properties. They are used like array-expressions. For instance, you could imagine creating your own "array" like this:

public class MyArray {
     string[] _storrage = new string[100];
     public string this[int index] {
         get { return _storrage[index]; }
         set { _storrage[index] = value; }
     }
}

You can create index-properties with multiple arguments as well, and they can be overloaded. For instance, some lists allows you to access items either by name ( this[string name] ) and index ( this[int index] ). In all actuality it's just syntactic sugar so you don't have to write methods like getItem(int index) as you do in java.

The other consept that it seems you're not understanding (though not apparent from your question) is lambda-methods. In your code:

(item) => item.GetSelectLink(item.Id)

is a lambda-method. For instance if the method Colum on your grid -object has the following signature:

public void Column<T>(string name, Func<T, string>)

the expression (item) => item.GetSelectLink(item.Id) would evaluate to a method that takes a T and returns a string. An alternative way to create the same functionality is to do this:

// in class
public string GetIdField(YourClassType item) { return item.GetSelectLink(item.Id); }

// in Column-call
grid.Column("Id", format: GetIdField);

You can read more about lambdas here: http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx

[Edit]
To show a quick example of a lambda that might shred some light on this:

var list = new List<string>() { "a", "b", "c" };
list.ForEach((item) => Console.WriteLine(item));

this will output "a", "b" and "c" (see it run at http://ideone.com/MGZxfr ). The equivalent (without using a lambda) would be something like this:

void Main() {
    var list = new List<string>() { "a", "b", "c" };
    foreach(var item in list) Print(item);
}

void Print(string item) { Console.WriteLine(item); }

It's not exactly a property, but a [] operator.
the get method will be called when you will do something like this:

var a = Ob["hello"]

and the set :

ob["hello"] = 5

它是一个索引器MSDN为您提供服务

This is called an indexer You can create your own indexer property as well.

It is commonly used in collections such as Array, List or Dictionary.

http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM