我们在我的团队中对是否应该将注入的依赖项存储为只读字段存在小分歧:
public class MyClient
{
private readonly IService service;
public MyClient(IService service)
{
this.service = service;
}
}
Run Code Online (Sandbox Code Playgroud)
或作为私有只读属性:
public class MyClient
{
private IService Service { get; }
public MyClient(IService service)
{
Service = service;
}
}
Run Code Online (Sandbox Code Playgroud)
一种选择比另一种选择有什么优势吗?我喜欢前者,但似乎我们班有一半使用后者。
c# ×1