Share via


SPFieldCollection.Item property (Int32)

Gets the field object at the specified index in the collection. In Microsoft C#, this property is an indexer for the SPFieldCollection class.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public ReadOnly Default Property Item ( _
    iIndex As Integer _
) As SPField
    Get
'Usage
Dim instance As SPFieldCollection
Dim iIndex As Integer
Dim value As SPField

value = instance(iIndex)
public SPField this[
    int iIndex
] { get; }

Parameters

  • iIndex
    Type: System.Int32

    A 32-bit integer that specifies the index of the field.

Property value

Type: Microsoft.SharePoint.SPField
An SPField object that represents the field.

Remarks

The Item property throws an ArgumentOutOfRangeException if the specified index is outside the valid range of indices for the collection.

Examples

The following code example iterates through the collection of fields for a list and displays the title and type of each field.

This example requires using directives (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

The example assumes the existence of an .aspx page that contains a label control.

Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("MySite")
Try
    Dim fields As SPFieldCollection = webSite.Lists("MyList").Fields
    Dim i As Integer

    For i = 0 To fields.Count - 1
        Label1.Text += SPEncode.HtmlEncode(fields(i).Title) + " :: " + fields(i).TypeAsString + "<BR>"
    Next i
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"])
{
    SPFieldCollection collFields = oWebsite.Lists["MyList"].Fields;

    for (int intIndex=0; intIndex<collFields.Count; intIndex++)
    {
        Label1.Text += SPEncode.HtmlEncode(collFields[intIndex].Title) + " :: " + collFields[intIndex].TypeAsString + "<BR>";
     }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also

Reference

SPFieldCollection class

SPFieldCollection members

Item overload

Microsoft.SharePoint namespace