说明下:参数PageIndex是从0开始;参数PageCount表示需要分页的数量;
using System;
using System.Collections.Generic;
public class MyClass
{
static int startIndex,endIndex;
static int pageSize=4;
static int pageCount=13;
public static void RunSnippet()
{
for(int i=0;i<21;i++)
g(i);
}
static void g(int pageIndex)
{
GetPageArea(ref startIndex,ref endIndex,pageIndex,pageSize,pageCount);
WL("Page{2}\tArea is : \t{0} \t-- \t{1}",startIndex,endIndex,pageIndex);
}
static void GetPageArea(ref int startIndex, ref int endIndex, int pageIndex, int pageSize, int pageCount)
{
if ( pageSize < 0 || pageCount < 0)
throw new ArgumentException("Maybe args too big!");
if (pageSize == 0 || pageCount == 0)
{
startIndex = 0;
endIndex = 0;
return;
}
if (pageIndex <= pageSize/2)
{
startIndex = 0;
endIndex = pageSize - 1;
return;
}
if (pageIndex > pageCount-pageSize/2-1)
{
startIndex = pageCount - pageSize;
endIndex = pageCount - 1;
return;
}
startIndex = pageIndex - pageSize / 2;
endIndex = pageIndex + pageSize / 2 - (pageSize % 2 == 1 ? 0 : 1);
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}