属性/方法定义

时间:2017-08-09 22:55:15

标签: c#

public class PaginationHelper<T> : IPagination<T>
{
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public IList<T> Items { get; set; }
    public IList<T> PaginationItems => AsPagination();
    public int TotalItems { get; set; }
    public int FirstItem => (PageNumber - 1) * PageSize + 1;

    public int LastItem => FirstItem + PageSize - 1;

    public int TotalPages
    {
        get
        {
            var result = (int)Math.Ceiling((double)TotalItems / PageSize);
            return result == 0 ? 1 : result;
        }
    }

    public bool HasPreviousPage => PageNumber > 1;

    public bool HasNextPage => PageNumber < TotalPages;

    public List<T> AsPagination()
    {
        var numberToSkip = (PageNumber - 1) * PageSize;
        var results = Items.Skip(numberToSkip).Take(PageSize).ToList();
        return results;
    }
}

public class Paginator<T>
{
    public List<T> PaginationItems { get; set; }
    public int TotalPages { get; set; }
}

我的朋友给了我这段代码,我无法编译。在C#中定义属性/方法时,是否可以这样使用? public int FirstItem =&gt; (PageNumber - 1)* PageSize + 1;这是正确的语法??

1 个答案:

答案 0 :(得分:8)

那句法;在C#6和VS 2015 C#编译器中引入了正式名称为表达体的成员

早期版本的Visual Studio无法编译它;您必须重构符合C#5的代码:

public int FirstItem { get { return (PageNumber - 1) * PageSize + 1; } }
相关问题