.NET 4.8项目中为何能使用C# 12的主构造函数?
主构造函数在.NET 4.8项目中的使用情况
主构造函数(primary constructor)是随C# 12与.NET 8一同推出的语言特性,但我在基于.NET 4.8的项目中,将项目的langVersion设置为Latest Major,搭配Autofac实现依赖注入时,却能正常使用主构造函数。
对应的代码示例如下:
public interface IService { Distance GetDistance(); } public class ExampleController(IService service) : ControllerBase { [HttpGet] public ActionResult<Distance> Get() { return service.GetDistance(); } }
内容的提问来源于stack exchange,提问作者Selmo Jack
相关产品推荐
相关产品推荐

