Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Console.Write (String, Object[])

 

Publicado: octubre de 2016

Escribe la representación de texto de la matriz de objetos especificada en el flujo de salida estándar usando la información de formato especificada.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static void Write(
	string format,
	params object[] arg
)

Parámetros

format
Type: System.String

Cadena de formato compuesto (vea Comentarios).

arg
Type: System.Object[]

Matriz de objetos que se va a escribir con format.

Exception Condition
IOException

Error de E/S.

ArgumentNullException

El valor de format o arg es null.

FormatException

La especificación de formato de format no es válida.

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. The resulting string is written to the output stream.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the text representation of the value of the corresponding object.

The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

  • For more information about the composite formatting feature supported by methods such as Overload:System.String.Format, Overload:System.Text.StringBuilder.AppendFormat, and some overloads of Overload:System.Console.WriteLine, see Composite Formatting.

  • For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

  • For more information about date and time format specifiers, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.

  • For more information about enumeration format specifiers, see Enumeration Format Strings.

  • For more information about formatting, see Formatting Types in the .NET Framework.

The arg parameter is a parameter array. Arguments can be passed to the method either as an array or as list of five or more items. The examples illustrate both forms of method call.

Notas para llamadores:

This method is not called by C++ code. The C++ compiler resolves calls to Overload:System.Console.Write that include a string and a list of four or more object parameters as a call to M:System.Console.Write(System.String,System.Object,System.Object,System.Object,System.Object). It resolves calls to Overload:System.Console.Write that include a string and an object array as a call to M:System.Console.Write(System.String,System.Object).

The following example defines a Person class with a number of properties that provide information about a person. Its GetDescription method returns an array that contains all of the property values except one. The example then uses the array returned by the GetDescription method to display the values of the Person object.

using System;

public class Person
{
   public String Name { get; set; }
   public DateTime BirthDate  { get; set; }
   public Double Height { get; set; }
   public Double Weight { get; set; }
   public Char Gender { get; set; }
   public String Remarks { get; set; }

   public object[] GetDescription() 
   {
      return new object[] { Name, Gender, Height, Weight, BirthDate};
   }
}

public class Example
{
   public static void Main()
   {
      var p1 = new Person() { Name = "John", Gender = 'M',
                              BirthDate = new DateTime(1992, 5, 10), 
                              Height = 73.5, Weight = 207 };
      p1.Remarks = "Client since 1/3/2012";
      Console.Write("{0}: {1}, born {4:d}  Height {2} inches, Weight {3} lbs  ", 
                    p1.GetDescription());
      if (String.IsNullOrEmpty(p1.Remarks))
         Console.WriteLine();
      else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
                              "\n   " : "");
   }
}
// The example displays the following output:
//    John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

Note that the example calls the M:System.Console.Write(System.String,System.Object[]) method rather than the M:System.Console.WriteLine(System.String,System.Object[]) method because it attempts to display the value of the Person.Remarks property on the same line. To do this, it examines the value of the P:System.Console.CursorLeft and P:System.Console.WindowWidth properties to determine whether there is enough space for the remark to fit. If there is, it displays the line. If not, it writes a line, indents three spaces, and displays the remark.

The following example is identical to the first, except that it supplies a five-item list as the arg argument instead of a parameter array.

using System;

public class Person
{
   public String Name { get; set; }
   public DateTime BirthDate  { get; set; }
   public Double Height { get; set; }
   public Double Weight { get; set; }
   public Char Gender { get; set; }
   public String Remarks { get; set; }

   public object[] GetDescription() 
   {
      return new object[] { Name, Gender, Height, Weight, BirthDate};
   }
}

public class Example
{
   public static void Main()
   {
      var p1 = new Person() { Name = "John", Gender = 'M',
                              BirthDate = new DateTime(1992, 5, 10), 
                              Height = 73.5, Weight = 207 };
      p1.Remarks = "Client since 1/3/2012";
      Console.Write("{0}: {1}, born {2:d}  Height {3} inches, Weight {4} lbs  ", 
                    p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight);
      if (String.IsNullOrEmpty(p1.Remarks))
         Console.WriteLine();
      else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
                              "\n   " : "");
   }
}
// The example displays the following output:
//    John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

UIPermission

for modifying safe top-level windows and subwindows. Associated enumeration: F:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows

.NET Framework
Disponible desde 1.1
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Volver al principio
Mostrar: