Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
65
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Section 12.8: Numeric formats

// Integral types as hex

string.Format("Hexadecimal: byte2: {0:x2}; byte4: {0:X4}; char: {1:x2}", 123, (int)'A');

// Integers with thousand separators

string.Format("Integer, thousand sep.: {0:#,#}; fixed length: >{0,10:#,#}<", 1234567);

// Integer with leading zeroes

string.Format("Integer, leading zeroes: {0:00}; ", 1);

// Decimals

string.Format("Decimal, fixed precision: {0:0.000}; as percents: {0:0.00%}", 0.12);

Output:

Hexadecimal: byte2: 7b; byte4: 007B; char: 41

Integer, thousand sep.: 1,234,567; fixed length: > 1,234,567<

Integer, leading zeroes: 01;

Decimal, fixed precision: 0.120; as percents: 12.00%

Section 12.9: ToString()

The ToString() method is present on all reference object types. This is due to all reference types being derived from Object which has the ToString() method on it. The ToString() method on the object base class returns the type name. The fragment below will print out "User" to the console.

public class User

{

public string Name { get; set; } public int Id { get; set; }

}

...

var user = new User {Name = "User1", Id = 5}; Console.WriteLine(user.ToString());

However, the class User can also override ToString() in order to alter the string it returns. The code fragment below prints out "Id: 5, Name: User1" to the console.

public class User

{

public string Name { get; set; } public int Id { get; set; } public override ToString()

{

return string.Format("Id: {0}, Name: {1}", Id, Name);

}

}

...

var user = new User {Name = "User1", Id = 5}; Console.WriteLine(user.ToString());

GoalKicker.com – C# Notes for Professionals

66

Section 12.10: Escaping curly brackets inside a String.Format() expression

string outsidetext = "I am outside of bracket"; string.Format("{{I am in brackets!}} {0}", outsidetext);

//Outputs "{I am in brackets!} I am outside of bracket"

Section 12.11: Relationship with ToString()

While the String.Format() method is certainly useful in formatting data as strings, it may often be a bit overkill, especially when dealing with a single object as seen below :

String.Format("{0:C}", money); // yields "$42.00"

An easier approach might be to simply use the ToString() method available on all objects within C#. It supports all of the same standard and custom formatting strings, but doesn't require the necessary parameter mapping as there will only be a single argument :

money.ToString("C"); // yields "$42.00"

Caveats & Formatting Restrictions

While this approach may be simpler in some scenarios, the ToString() approach is limited with regards to adding left or right padding like you might do within the String.Format() method :

String.Format("{0,10:C}", money); // yields " $42.00"

In order to accomplish this same behavior with the ToString() method, you would need to use another method like PadLeft() or PadRight() respectively :

money.ToString("C").PadLeft(10); // yields " $42.00"

GoalKicker.com – C# Notes for Professionals

67