03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

566 ❘ CHAPTER 25 Serialization<br />

}<br />

}<br />

Description = description;<br />

Quantity = quantity;<br />

UnitPrice = unitPrice;<br />

This class has Description, Quantity, and UnitPrice fields; a parameterless constructor; and an<br />

initializing constructor.<br />

The following code shows the Order class.<br />

public class Order<br />

{<br />

public DateTime OrderDate;<br />

public OrderItem[] OrderItems;<br />

}<br />

public Order() { }<br />

public Order(DateTime orderDate, params OrderItem[] orderItems)<br />

{<br />

OrderDate = orderDate;<br />

OrderItems = orderItems;<br />

}<br />

This class includes an OrderDate field and an array of OrderItem objects. It also has a parameterless<br />

constructor and an initializing constructor.<br />

The following code shows the Person class.<br />

// Customer and related classes.<br />

public class Customer<br />

{<br />

public string FirstName, LastName;<br />

public List Orders = new List();<br />

}<br />

public Customer() { }<br />

public Customer(string firstName, string lastName, params Order[] orders)<br />

{<br />

FirstName = firstName;<br />

LastName = lastName;<br />

foreach (Order order in orders) Orders.Add(order);<br />

}<br />

This class has FirstName and LastName fields. It holds information about orders in a List.<br />

When the program starts, it uses the following code to create a Customer object and display its data.<br />

// Create some OrderItems.<br />

OrderItem item1 = new OrderItem("Pencil", 12, 0.25m);<br />

OrderItem item2 = new OrderItem("Notepad", 6, 1.00m);<br />

OrderItem item3 = new OrderItem("Binder", 1, 3.50m);<br />

OrderItem item4 = new OrderItem("Tape", 12, 0.75m);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!