13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

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.

Chapter 3: C# and <strong>ASP</strong>.<strong>NET</strong> <strong>3.5</strong> 51calculations and even the variable typing. For example, the following statements areexpressions that type a variable (declare a variable type) and assign it a value made up ofcalculated values and another variable (anotherNumber) and a <strong>com</strong>bination of stringliterals and a string variable (anotherString):int myNumber = 45 + (anotherNumber - 3);string myString = "Name: " + anotherString + " Smith";Once you have set up your variable, you can run into problems as you attempt to assignthe wrong kind of value to a variable. For example, the following assignments wouldthrow errors:int myNumber = "Calculate this: 2 * 10";string myString = 33 * 22;In the first case, the numeric variable is assigned a string literal. (A literal is an actualvalue.) Any expression enclosed in quotes is treated as a string literal. In the second case,not only are the values for a string numeric values, but also the expression contains anumeric operator (*) for multiplication.Type DetailsDifferentiating between numbers and strings is pretty easy. Thing get a little trickywhen you begin looking at the different data types. With text, the two basic data typesare string and char. The char data type holds a single text character such as a, b,c, 5, or @. (Each character is a Unicode character.) A string data type contains aseries of Unicode characters, such as “My dog Fred lives @ home!” The char data typerequires less memory reserved because each value is the same length. The string datatype requires more memory because the amount of memory depends on the number ofcharacters. Also, just as numbers and strings are different, so are char and string types.For example, the following would throw an error:char myCharacter = "Top of the morning!"; //Error!because more than a single Unicode character is in the value assigned to a char typevariable. Most of the C# examples you will see in this book use the string type.With numeric types, you will find far more types. In the most basic sense, you candivide number types into whole numbers (integers) and floating-point numbers (containfractions). You can also divide numeric types into signed (may have both positive andnegative values) and unsigned (positive numbers only). Finally, the types you encountereither are built-in (intrinsic) or as created from classes and interfaces. You will be usingintrinsic and class-based types.

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

Saved successfully!

Ooh no, something went wrong!