c# - Difference between property Users and IRepository.Users -
i have interface irepository
abstract repository. dbcontext
class entity framework work first code.
public interface irepository { iqueryable<user> users { get; } } public class repository : dbcontext, irepository { public dbset<user> users { get; set; } iqueryable<user> irepository.users { get{ return users; } } }
i did not understand user
property user in repository
class.
this code compiles, wonder why.
what interface name before name of property?
this valid syntax, called explicit interface implementation. it's useful in 2 situations:
- when implement 2 interfaces same method (check link above)
- when want "hide" implementation detail in sense. in example, if user have
dbcontext
reference, seedbset<user>
property , able access it, on other hand, if give userirepository
- seeiqueryable<user>
, let's hide what's underneath it.
//edit: actually, 1 more usage , used in code well:
- you can hide methods / properties still implementg them. example,
dictionary<k, v>
implementsicollection<keyvaluepair<k, v>>
, it's ought implement method signature:void add(keyvaluepair<k, v> kv)
, 1 ugly, prefervoid add(k key, v value)
, can't ignore first one. explicit int. impl. can implement first method, hide user (at least long won't cast dictionary icollection).
Comments
Post a Comment