Make a shortcut for Console.WriteLine()

前端 未结 14 1428
北恋
北恋 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:33

    If you write this at the top of the page:

    using j = System.Console;
    

    then at any time, you can use

    j.WriteLine("Anything you want to write");
    

    And that's all.

    By the way, you can use anything instead of the "j".

    0 讨论(0)
  • 2020-12-23 20:35

    To piggyback on Michael Stum's answer, we could also make object as the type parameter to Action delegate like so:

    Action<object> cw = x => Console.WriteLine(x.ToString());
    

    I usually do that on my C# Interactive window to quickly print out objects that I'm working with.

    For example:

    > var grid = driver.FindElements(By.XPath("//div[@class='ReactVirtualized__Grid__innerScrollContainer']//div"));
    > cw(grid);
    System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]
    > 
    
    0 讨论(0)
  • 2020-12-23 20:35
    // For formatting string and parameters define a function
    // The shortcut function wl, kind of write line
    public void wl( string format, params object[] parms){
        Console.WriteLine(format, parms);
    }
    
    // Just for strings we can use Action delegate
    Action<string> ws = Console.WriteLine;
    
    // examples:
    ws("String with no formatting parameters");
    
    wl("String without formatting parameters");
    wl("String with {0} parameters {1}", 2, "included");
    wl("several parameters {0} {1} {2} repeated {0}", 1234, 5678, 6543);
    

    or using extension method: formatString.wl(arguments...)

    public static class ConsoleWriteExtensions
    {
        public static void wl(this string format, params object[] parms){
            Console.WriteLine(format, parms);
        }
    }
    
    "{0} -> {1}".wl("Mili",123.45); // prints Mili -> 123.45
    
    0 讨论(0)
  • 2020-12-23 20:36

    If you want it global, you could write an extension method:

    public static class StringExtensions
    {
       public static void ConLog(this string msg)
       {
         Console.WriteLine(msg);
       }
    }
    

    Now wherever you are, you can call "My Message".ConLog(); on any string in your application and write it to the console.

    0 讨论(0)
  • 2020-12-23 20:38
    public static void CW(string str)
    {
         Console.WriteLine(str);
    }
    
    0 讨论(0)
  • 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}" );
    
    0 讨论(0)
提交回复
热议问题