# The C# Cursor Class

`Cursor` provides methods for iterating through instances of a class.

### For an overview see page C# Classes

## Class Definition

        public enum Operation {
            LessThan, LessOrEquals, Equals, GreaterOrEquals, GreaterThan,
            Overlaps, Contains, ExactMatch, BestMatch, PrefixMatch,
            NextMatch, Neighbourhood, StrictEquals, IsPrefixOf, ContainsAll, ContainsAny
        }

        public interface ICursor : IEnumerable, IEnumerator {
            bool Checkpoint();
            void Update();
            void Remove();
            long GetAutoId();
        }

        public class Cursor<T> : ICursor, IEnumerable<T>, IEnumerator<T> where T : class
        {
            // --- Constructors ---
            public Cursor(Connection con, String indexName);
            public Cursor(Connection con);

            // --- Properties ---
            public T Current { get; }

            // --- Enumerator Methods ---
            public IEnumerator<T> GetEnumerator();

            // --- Search & Query Methods ---
            public bool Search(Operation op, params Object[] val);
            public T Find(params Object[] val);
            public bool QueryByExample(object from, object till);

            // --- Navigation Methods ---
            public bool MoveFirst();
            public bool MoveLast();
            public bool MoveNext();
            public bool MovePrev();
            public T Skip(int offset);

            // --- Data Modification Methods ---
            public void Update();
            public void Remove();
            public bool Checkpoint();

            // --- Utility Methods ---
            public long GetAutoId();
            public void Store();
            public void Close();
            public void Reset();
            public void Dispose();

            // --- Nested Public Enums ---
            public enum Position { Unknown, Empty, First, Curr, DeleteNext, DeletePrev }
            public enum Direction { Unknown, Forward, Backward }
        }

## Method Descriptions

Operation enum
Defines search operations for database queries including comparison operators, spatial operations, and pattern matching.
ICursor.Checkpoint()
Commits the current object by inserting indexes for the updated object previously returned by navigation methods. Returns `false` if an MVCC conflict occurs.
ICursor.Update()
Persists changes to the current object previously returned by `MoveFirst()`, `MoveLast()`, `MoveNext()`, `MovePrev()`, or `Skip()`.
ICursor.Remove()
Deletes the current object from the database. The object must have been previously returned by a navigation method.
ICursor.GetAutoId()
Returns the AUTOID of the current object if one exists. The object must have been previously returned by a navigation method.
Cursor(Connection con, String indexName)
Initializes a cursor to iterate through instances of type `T` using the specified named index. Inheritance relationships are not considered.
Cursor(Connection con)
Initializes a cursor to iterate through instances of type `T` using the class's AUTOID or list index. The class must have `list()` or `autoid()` attributes configured in its Persistent annotation.
Current
Gets the current element of type `T` at the cursor position. Returns `null` if no valid current object exists.
GetEnumerator()
Returns an enumerator for iterating through cursor results. Note: all enumerators share the same underlying eXtremeDB cursor, so creating multiple enumerators for the same cursor is not recommended.
Search(Operation op, params Object[] val)
Executes a search operation and positions the cursor at the first matching element. Supported operations depend on index type: Hashtable (Equals), BTree (comparison), RTree (spatial), Patricia (pattern), Trigram (Contains).
Find(params Object[] val)
Performs an exact-match lookup on a unique index. Equivalent to `Search(Equals, value)` followed by `MoveNext()`. Returns the matching object or `default(T)` if not found.
QueryByExample(object from, object till)
Executes a Query-By-Example search for KD-Tree indexes. Applicable only to classes generated by CILGenerator. Fields set in `from` define inclusive lower bounds; fields in `till` define inclusive upper bounds.
MoveFirst()
Positions the cursor at the first object in the result set. Returns `true` if the set is not empty.
MoveLast()
Positions the cursor at the last object in the result set. Returns `true` if the set is not empty.
MoveNext()
Advances the cursor to the next object in the result set. Returns `true` if successful, `false` if no more objects remain.
MovePrev()
Moves the cursor to the previous object in the result set. Returns `true` if successful, `false` if no more objects remain.
Skip(int offset)
Skips the specified number of results. Positive values move forward, negative values move backward. Returns the object at the target position or `default(T)` if unreachable.
Update()
Persists modifications to the current object in the database. The object must have been previously returned by a navigation method.
Remove()
Deletes the current object from the database. Requires a valid current object obtained via navigation methods.
Checkpoint()
Commits the current object and updates its indexes. Returns `false` if an MVCC conflict prevents the operation.
GetAutoId()
Retrieves the AUTOID of the current object. Throws an exception if the class lacks an AUTOID index or no current object exists.
Store()
Saves the cursor state for reuse in a subsequent transaction.
Close()
Closes the cursor and releases associated native resources.
Reset()
Resets the cursor to its initial state. Call `MoveFirst()` or `MoveLast()` to resume iteration.
Dispose()
Releases resources held by the cursor. Implements the `IDisposable` pattern.
Position enum
Internal state enum tracking cursor position: `Unknown`, `Empty`, `First`, `Curr`, `DeleteNext`, `DeletePrev`.
Direction enum
Internal enum indicating cursor traversal direction: `Unknown`, `Forward`, `Backward`.