Naming convention - underscore in C++ and C# variables

前端 未结 19 2328
春和景丽
春和景丽 2020-11-28 01:11

It\'s common to see a _var variable name in a class field. What does the underscore mean? Is there a reference for all these special naming conventions?

19条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 01:55

    Old question, new answer (C#).

    Another use of underscores for C# is with ASP NET Core's DI (dependency injection). Private readonly variables of a class which got assigned to the injected interface during construction should start with an underscore. I guess it's a debate whether to use underscore for every private member of a class (although Microsoft itself follows it) but this one is certain.

    private readonly ILogger _logger;
    
    public MyDependency(ILogger logger)
    {
        _logger = logger;
    }
    

提交回复
热议问题