编写环境:SnippetCompiler

不需要过多的解释,如有疑问请留言。

using System;

using System.Collections.Generic;

public class MyClass

{

static int count=1234;

    static int pageSize=10;

static int startIndex,endIndex;

    public static void RunSnippet()

{

        GetDataArea(0);

GetDataArea(1);

        GetDataArea(45);

GetDataArea(123);

        GetDataArea(124);

GetDataArea(1999);

    }

    private static void GetDataArea(int pageIndex)

{

        GetDataArea(ref startIndex,ref endIndex,pageIndex,pageSize,count);

Console.WriteLine("Page{2}\tArea is : \t{0} \t-- \t{1}",startIndex,endIndex,pageIndex);

    }

    private static void GetDataArea(ref int startIndex,ref int endIndex,int pageIndex,int pageSize,int count)

{

        if(pageSize<=0||pageIndex<0||count<=0)

throw new ArgumentException ("All paramater should bigger than zero!");

try

        {

startIndex=pageIndex*pageSize;

            endIndex=startIndex+pageSize-1;

}

        catch

{

            throw new ArgumentOutOfRangeException("May be the argument too bigger than Design!");

}

        if(startIndex+1>count)

{

            GetDataArea(ref startIndex,ref endIndex,pageIndex-1,pageSize,count);

return;

        }

if(count<endIndex+1)

            endIndex=count-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

}


作者:KKcat