Console.WriteLine メソッド (String, Object, Object)

指定した書式情報を使用して、指定したオブジェクトを標準出力ストリームに書き込み、続けて現在の行終端記号を書き込みます。

Overloads Public Shared Sub WriteLine( _
   ByVal format As String, _   ByVal arg0 As Object, _   ByVal arg1 As Object _)
[C#]
public static void WriteLine(stringformat,objectarg0,objectarg1);
[C++]
public: static void WriteLine(String* format,Object* arg0,Object* arg1);
[JScript]
public static function WriteLine(
   format : String,arg0 : Object,arg1 : Object);

パラメータ

  • format
    書式指定文字列。
  • arg0
    format を使用して書き込む最初のオブジェクト。
  • arg1
    format を使用して書き込む 2 番目のオブジェクト。

例外

例外の種類 条件
IOException I/O エラーが発生しました。
ArgumentNullException format が null 参照 (Visual Basic では Nothing) です。
FormatException format の書式指定が無効です。

解説

このメソッドは String.Format と同じセマンティクスを使用します。

パラメータ オブジェクトが format で参照されない場合、これは無視されます。

行終端記号の詳細については、パラメータを受け取らない WriteLine メソッドの解説を参照してください。

WriteLine の使用方法については、次のコード例を参照してください。

 
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Public Shared Function Main(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         Try
            billTotal = Double.Parse(args(1))
         Catch
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End Try
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:" + ControlChars.Tab + "{0,8:c}", billTotal)
         Console.WriteLine("Tip total/rate:" + ControlChars.Tab + "{0,8:c} ({1:p1})", tip, tipRate)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:" + ControlChars.Tab + "{0,8:c}", billTotal + tip)
         Return 0
      End If
   End Function 'Main
End Class 'TipCalculator

[C#] 
public class TipCalculator {
    private const double tipRate = 0.18;
    public static int Main(string[] args) {
        double billTotal;
        if (args.Length == 0) {
            Console.WriteLine("usage: TIPCALC total");
            return 1;
        }
        else {
            try {
                billTotal = Double.Parse(args[0]);
            }
            catch(FormatException) {
                Console.WriteLine("usage: TIPCALC total");
                return 1;
            }
            double tip = billTotal * tipRate;
            Console.WriteLine();
            Console.WriteLine("Bill total:\t{0,8:c}", billTotal);
            Console.WriteLine("Tip total/rate:\t{0,8:c} ({1:p1})", tip, tipRate);
            Console.WriteLine(("").PadRight(24, '-'));
            Console.WriteLine("Grand total:\t{0,8:c}", billTotal + tip);
            return 0;
        }
    }
}

[C++] 
int main() {
String* args[] = Environment::GetCommandLineArgs();
    const double tipRate = 0.18;

    double billTotal;
    if (args->Length != 2) {
        Console::WriteLine(S"usage: TIPCALC total");
        return 1;
    }
    else {
        try {
            billTotal = Double::Parse(args[1]);
        }
        catch(FormatException*) {
            Console::WriteLine(S"usage: TIPCALC total");
            return 1;
        }
        double tip = billTotal * tipRate;
        Console::WriteLine();
        Console::WriteLine(S"Bill total:\t{0,8:c}", __box(billTotal));
        Console::WriteLine(S"Tip total/rate:\t{0,8:c} ({1:p1})", __box(tip), __box(tipRate));
        Console::WriteLine((S"")->PadRight(24, '-'));
        Console::WriteLine(S"Grand total:\t{0,8:c}", __box(billTotal + tip));
        return 0;
    }
}

[JScript] 

const tipRate : Number = 0.18;
var billTotal : Number;
var args : String[] = Environment.GetCommandLineArgs();

if (args.Length != 2) {
Console.WriteLine("usage: TIPCALC total");
Environment.Exit(1);
}

try {
billTotal = Double.Parse(args[1]);
}
catch(FormatException) {
Console.WriteLine("usage: TIPCALC total");
Environment.Exit(1);
}

var tip : double = billTotal * tipRate;
Console.WriteLine();
Console.WriteLine("Bill total:\t{0,8:c}", billTotal);
Console.WriteLine("Tip total/rate:\t{0,8:c} ({1:p1})", tip, tipRate);
Console.WriteLine(("").PadRight(24, '-'));
Console.WriteLine("Grand total:\t{0,8:c}", billTotal + tip);

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Console クラス | Console メンバ | System 名前空間 | Console.WriteLine オーバーロードの一覧 | 書式設定の概要 | Read | ReadLine | Write