In C#: Why no 'Item' on System.Data.DataRow?

后端 未结 2 2142
难免孤独
难免孤独 2020-12-16 20:54

I\'m rewriting/converting some VB-Code:

Dim dt As New System.Data.DataTable()
Dim dr As System.Data.DataRow = dt.NewRow()
Dim item = dr.Item(\"myItem\")


        
相关标签:
2条回答
  • 2020-12-16 21:33

    Try like this:

    var item = dr["myItem"];
    

    In C# you can access the indexer property directly. And the DataRow.Item property is defined as indexer.

    0 讨论(0)
  • 2020-12-16 21:49

    There is actually no "Item" property in C#. In VB the DataRow cell access is defined like this:

    Default Public Property Item (
        column As DataColumn
    ) As Object
    

    So there is a literal "Item" property. However, in C# it is defined like this:

    public object this[
        DataColumn column
    ] { get; set; }
    

    So this is the default property of the class / object. So you access it with the object name.

    0 讨论(0)
提交回复
热议问题