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.

736 ❘ Appendix B Data Types<br />

Casting Arrays<br />

You can cast variables of array types. For example, an array of Student objects is also an array of<br />

Person objects because a Student is a type of Person. The following code demonstrates implicit<br />

and explicit array conversions.<br />

// Make an array of Students.<br />

Student[] students = new Student[10];<br />

// Implicit cast to an array of Persons.<br />

// (A Student is a type of Person.)<br />

Person[] persons = students;<br />

// Explicit cast back to an array of Students.<br />

students = (Student[])persons;<br />

Parsing Values<br />

Data types have parsing methods that convert string values into values of the data type. For example,<br />

the following code parses the text in the TextBox named numValuesTextBox and saves the result in the<br />

integer variable numValues.<br />

int numValues = int.Parse(numValuesTextBox.Text);<br />

The data types also provide TryParse methods that attempt to parse a string and return a boolean<br />

value indicating whether they succeeded.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!