Curious about syntax

C

C.

Hi,

I'm curious about some syntax I'm seeing in an object someone wrote
used to paginate through a collection. It's all straightforward to me
except the last method:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

I'm confused about that System.Collections.IEnumerable.GetEnumerator()
definition when the pagination object already defines a GetEnumerator
function. Since you can't have an overloaded method if only the return
types differ, I'm confused by the voodoo going on here. The complete
Pagination object is as follows:


/// <summary>
/// Summary description for Pagination
/// </summary>
public class Pagination<T> : IEnumerable<T>
{
private IEnumerable<T> _collection;

/// <summary>
/// Initializes a new instance of the <see
cref="PageableCollection&lt;T&gt;"/> class.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="requestedCount">The requested count.</param>
/// <param name="totalCount">The total count.</param>
public Pagination(IEnumerable<T> collection, int startIndex, int
requestedCount, int totalCount)
{
_collection = collection;
StartIndex = startIndex;
PageSize = requestedCount;
TotalCount = totalCount;
}

/// <summary>
/// Gets the total item count.
/// </summary>
/// <value>The total item count.</value>
public virtual int TotalCount { get; protected internal set; }

/// <summary>
/// Gets the size of the page.
/// </summary>
/// <value>The size of the page.</value>
public virtual int PageSize { get; protected internal set; }

/// <summary>
/// Gets or sets the start index.
/// </summary>
/// <value>The start index.</value>
public virtual int StartIndex { get; protected internal set; }

/// <summary>
/// Gets the current page number.
/// </summary>
/// <value>The page number.</value>
public virtual int PageNumber
{
get { return (StartIndex / PageSize) + 1; }
}

/// <summary>
/// Gets the total page count.
/// </summary>
/// <value>The total page count.</value>
public virtual int PageCount
{
get { return (int)Math.Ceiling((double)TotalCount /
(double)PageSize); }
}

#region IEnumerable<T> Members

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/>
that can be used to iterate through the collection.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
return _collection.GetEnumerator();
}

#endregion

#region IEnumerable Members

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that
can be used to iterate through the collection.
/// </returns>
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

#endregion
}
 
P

Peter Duniho

C. said:
Hi,

I'm curious about some syntax I'm seeing in an object someone wrote
used to paginate through a collection. It's all straightforward to me
except the last method:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

I'm confused about that System.Collections.IEnumerable.GetEnumerator()
definition when the pagination object already defines a GetEnumerator
function.

I think the comments for the code are pretty clear. The first
GetEnumerator() method implements IEnumerable<T>. The second implements
IEnumerable. Note that the method names are _not_ the same. The first
is simply "GetEnumerator". The second is
"System.Collections.IEnumerable.GetEnumerator".

The latter is the syntax for declaring an interface implementation
explicitly. Such implementations can be used _only_ by casting the
variable to the interface type; they aren't available in the declaring
type itself.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top