一个求分页的函数

编写环境: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
}
posted @   today4king  阅读(249)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示
主题色彩