小编Sco*_*man的帖子

C#.是否可以使用具有基类型约束的静态泛型类,该基类型约束具有带有进一步基类型约束的方法

我希望能够创建一个基类型约束的静态泛型类型

public static class Manager<T> where T : HasId
{
    public static T GetSingleById(ref List<T> items, Guid id)
    {
        // the Id is a property provided by HasId
        return (from i in items where i.Id == id select i).SingleOrDefault();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后添加另一种方法

...
    public static IEnumerable<T> GetManyByParentId(ref List<T> items, Guid parentId) where T : HasIdAndParentId
    {
        // the parentId is a property of HasIdAndParentId which subclasses HasId
        return from i in items where i.ParentId == parentId select i;
    }
... …
Run Code Online (Sandbox Code Playgroud)

c# generics types constraints base

3
推荐指数
1
解决办法
217
查看次数

在Visual Studio 2015中,可以设置只读自动属性并构建它!这是一个错误吗?

这段代码

public class Entity
{
    public string First { get; }
    public string Last { get; }
    public Entity()
    {
        this.First = "First name";
        this.Last = "Last name";
    }
}
Run Code Online (Sandbox Code Playgroud)

将在VS2015中编译而VS2013会出现错误"属性或索引器'ScottRickman.Entity.First'无法分配给它 - 它是只读的"

这是VS2015中的错误吗?

c# visual-studio-2015

1
推荐指数
1
解决办法
524
查看次数

标签 统计

c# ×2

base ×1

constraints ×1

generics ×1

types ×1

visual-studio-2015 ×1