Clever way to append 's' for plural form in .Net (syntactic sugar)

前端 未结 14 1759
抹茶落季
抹茶落季 2021-01-30 06:53

I want to be able to type something like:

Console.WriteLine(\"You have {0:life/lives} left.\", player.Lives);

instead of

Consol         


        
14条回答
  •  自闭症患者
    2021-01-30 07:22

    string message = string.format("You have {0} left.", player.Lives == 1 ? "life" : "lives");
    

    Of course this assumes that you have a finite number of values to pluralize.

提交回复
热议问题