You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 04:17:01