有人可以向我解释这些陈述在C#中的意思吗?:
class RepositoryBase : IDisposable, IRepositoryBase where TEntity : class{}
class DbContextFactory : IContextFactory where TContext : DbContext, new()
我了解RepositoryBase
继承自IDisposable
和IRepositoryBase
类。它后面的东西是什么意思?
答案 0 :(得分:1)
这些都是类型约束的示例。您可以从the official documentation了解更多相关信息:
where T : class
:type参数必须是引用类型;这也适用于任何类,接口,委托或数组类型。
where T : new()
:type参数必须具有公共无参数构造函数。与其他约束一起使用时,必须最后指定 new()约束。
where T : <base class name>
:type参数必须是或来自指定的基类。
所以在你的情况下:
class RepositoryBase<TEntity> : /* etc */ where TEntity : class{}
表示TEntity
中的RepositoryBase<TEntity>
必须是引用类型,即对象。
对于第二种情况:
class DbContextFactory<TContext> : /* etc */ where TContext : DbContext, new()
表示TContext
必须是DbContext
的实例,并且TContext
必须具有
public TContext() { /* etc */ }
原帖海报说:
我了解
RepositoryBase
继承自IDisposable
和IRepositoryBase
类。它后面的东西是什么意思?
声明
class RepositoryBase : IDisposable, IRepositoryBase
表示RepositoryBase
实施 IDisposable
和IRepositoryBase
接口 。 RepositoryBase
不会从中继承 ,因为它们不是可以继承的类#34;来自(在C#意义上)。
相反,IDisposable
和IRepositoryBase
都是 接口 ,它们仅将行为和方法指定为合同,但不指定实现。在C#中,将所有接口命名为以大写I
前缀开头的惯例是惯例。
答案 1 :(得分:0)
你不是说
public class RepositoryBase<TEntity> : IDisposable, IRepositoryBase where TEntity : class{}
然后它只是意味着TEntity
泛型类型被认为是一个类,就是全部。
答案 2 :(得分:-1)
RepositoryBase实现了接口IDisposable和IRepositoryBase