what is the printf in C#

90,359

There is no direct "printf" duplication in C#. You can use PInvoke to call it from a C library.

However there is

Console.WriteLine("args1: {0} args2: {1}", value1, value2);

Or

Console.Write("args1: {0} args2: {1}", value1, value2);

Or

Console.WriteLine(string.Format("args1: {0} args2: {1}", value1, value2));

Or

Console.Write(string.Format("args1: {0} args2: {1}", value1, value2));

Or (C#6+ only)

Console.WriteLine($"args1: {value1} args2: {value2}");

Or (C#6+ only)

Console.Write($"args1: {value1} args2: {value2}");
Share:
90,359

Related videos on Youtube

user2387220
Author by

user2387220

Updated on August 31, 2021

Comments

  • user2387220
    user2387220 almost 3 years

    I want to know what to use in C# to format my output in my console window I tried to use \t but it did not work

    I know there is printf in C to format my output

    check this image https://s15.postimg.cc/94fstpi2z/Console.png

    • Lucas Trzesniewski
      Lucas Trzesniewski over 9 years
      string.Format, or directly the relevant overload of Console.WriteLine
  • BradleyDotNET
    BradleyDotNET over 9 years
    Note that the first two call String.Format. There is no functional difference between your two versions.
  • Bauss
    Bauss over 9 years
    I know, it was just for demonstration, because sometimes you'd like to format the string and maybe do some other manipulation to it before writing to the standard output stream.
  • Mafii
    Mafii over 7 years
    Maybe, for the sake of completness, add an example with interpolated strings (just mention its available in C#6 and not before)
  • Bauss
    Bauss over 7 years
    Added interpolated strings example now :)