Make a shortcut for Console.WriteLine()

前端 未结 14 1462
北恋
北恋 2020-12-23 19:55

I have to type Console.WriteLine() many times in my code. Is it possible to create a shortcut for Console.WriteLine so that I can use it like...

CW=Console.Wr         


        
14条回答
  •  盖世英雄少女心
    2020-12-23 20:38

    One of my favorites also... Coming from BASIC and Python... I missed Print() very often... I also use Print() extensively in JS/ES for console.log/other-consoles often...

    So declare it as a function:

    public static void Print( object x ){ Console.WriteLine( x ); }
    
    • you can compose strings at your will as the function parameter like:
     Print( "Hi\n\n" + x.toString() + "\n\nBye!!!" );
    
    • or interpolate vars at will
    Print( $"{x} ~ {y} ~ {z}" );
    

提交回复
热议问题