1

is there a way in C# to rename System.Console.WriteLine so that in my C#/console/visual studio program I could write

printf("hello");
//instead of 
System.Console.WriteLine("Hello");   //??

What would this be, a class? a namespace? Do I do this in the header or inside of main?

  • 2
    Why would you need that? – Yosi Dahari Sep 05 '13 at 14:38
  • 6
    No, but you could just use C instead of C#. – Dan Sep 05 '13 at 14:38
  • 2
    Why you want to do that ? why not add a wrapper class to do this ? – Kurubaran Sep 05 '13 at 14:39
  • Couldn't you just wrap `System.Console.WriteLine()` in a function of your own called `printf()`? Isn't this going to confuse people trying to maintain your code? – Lynn Crumbling Sep 05 '13 at 14:39
  • 2
    Why not just create a function with the relevant name you are looking to use. But please note that starting any function name with a lower case letter is not c# convention, so rather use Printf if you are going this route. – Andre Lombaard Sep 05 '13 at 14:40
  • is there nothing like the functional `let myAdd = fun x y -> x + y` or `let printf = fun x -> System.Console.WriteLine("x")` ? your favorite succinct tutorial on C# wrappers? – בנימן הגלילי Sep 05 '13 at 14:42
  • Why not just be okay with `Console.WriteLine`? There is no benefit to writing a method that simply wraps another public method. – Dan Sep 05 '13 at 14:42
  • 3
    In other news, look for a future post on thedailywtf.com! – Lynn Crumbling Sep 05 '13 at 14:42
  • possible duplicate of [How can global function exist in C#?](http://stackoverflow.com/questions/7296106/how-can-global-function-exist-in-c) – spender Sep 05 '13 at 14:44

7 Answers7

7

Write a wrapper:

public string printf(string theString)
{
  System.Console.WriteLine(theString);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 3
    ... you forgot the obligatory `// warning to future programmers: Prepare to be confused` :) – Lynn Crumbling Sep 05 '13 at 14:40
  • 4
    Clearly the OP needs this link: http://www.thc.org/root/phun/unmaintain.html (and sorry, in advance, to any dev's morning/day I just wasted because they went off and read this.) – Lynn Crumbling Sep 05 '13 at 14:44
5

You can use code snippets: http://msdn.microsoft.com/en-us/library/ms165392.aspx You can always write your own.

I think Visual Studio has a few by default, like writing cw and hitting TAB to get System.Console.WriteLine

Edit Here is a list of the default VS code snippets: http://msdn.microsoft.com/en-us/library/z41h7fat(v=vs.90).aspx

You can also use ReSharper to easily write your own, like this one I did for dw -> Debug.WriteLine

enter image description here

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    +1 assuming this question was to avoid keystrokes and not just to be sentimental about C. – juharr Sep 05 '13 at 16:54
3

Your target looks strange and there is no way to do this in c#. Alternatively you can add a method in a class and use it.

private static void printf(string value)
{
    System.Console.WriteLine(value);
}

Note: above method works only in the class which you write the above method.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

C# 6.0 Simplifies this by using static directives. Now instead of typing all of this: System.Console.WriteLine(...), you could simply type: WriteLine(...)

enter image description here

using static System.Console;

namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hello");
        }
    }
}

More Info: C# : How C# 6.0 Simplifies, Clarifies and Condenses Your Code

Jaider
  • 14,268
  • 5
  • 75
  • 82
1

You can wrap your code in a function, for example

public void Printf(string message)
{
      System.Console.Writeline(message);
}

It is not recommended that you start your function with a lowercase letter (for example printf) as this is not a c# convention.

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
1

First of all , do this is not a good practice.

But programatically this is achievable by wrting Wraps.

public string printf(string urstring)
{ 
System.Console.WriteLine(urstring);
}
Rupesh
  • 545
  • 2
  • 8
  • 19
0

If you want to get fancy you can do this:

static class DemoUtil
{
    public static void Print(this object self)
    {
        Console.WriteLine(self);
    }

    public static void Print(this string self)
    {
        Console.WriteLine(self);
    }
}

Then it would become:

"hello".Print();
12345.Print();
Math.Sin(0.345345).Print();
DateTime.Now.Print();

And so on. I don't recommend this for production code - but I do use this for my test code. I'm lazy.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276