Indexers in List vs Array

前端 未结 6 2021
误落风尘
误落风尘 2020-12-10 04:30

How are the Indexers are defined in List and Arrays.

List lists=new List(); where MyStruct is a Structure.

6条回答
  •  鱼传尺愫
    2020-12-10 04:53

    I ran into this as well, when I was inspecting lambda expression types. When the lambda is compiled into an expression tree you can inspect the expression type for each node. It turns out that there is a special node type ArrayIndex for the Array indexer:

    Expression> expression = () => new string[] { "Test" }[0];
    Assert.Equal(ExpressionType.ArrayIndex, expression.Body.NodeType);
    

    Whereas the List indexer is of type Call:

    Expression> expression = () => new List() { "Test" }[0];
    Assert.Equal(ExpressionType.Call, expression.Body.NodeType);
    

    This just to illustrate that we can reason about the underlying architecture with lambda expressions.

提交回复
热议问题