Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Q&A

Why shouldn't all numbers be declared as the larger data types instead of the smaller data types?

Although it might seem logical to use the larger data types, this would not be efficient. You should not use any more system resources (memory) than you need.

What happens if you assign a negative number to an unsigned variable?

You get an error by the compiler saying you can't assign a negative number to an unsigned variable if you do this with a literal. If you do a calculation that causes an unsigned variable to go below 0, you get erroneous data. On later days, you will learn how to check for these erroneous values.

A decimal value is more precise than a float or a double value. What happens with rounding when you convert from these different data types?

When converting from a float, double, or decimal to one of the whole number variable types, the value is rounded. If a number is too big to fit into the variable, an error occurs.

When a double is converted to a float that is too big or too small, the value is represented as infinity or 0, respectively.

When a value is converted from a float or double to a decimal, the value is rounded. This rounding occurs after 28 decimal places and occurs only if necessary. If the value being converted is too small to be represented as a decimal, the new value is set to 0. If the value is too large to store in the decimal, an error occurs.

For conversions from decimal to float or double, the value is rounded to the nearest value the float or double can hold. Remember, a decimal has better precision than a float or a double. This precision is lost in the conversion.

What other languages adhere to the Common Type System (CTS) in the Common Language Runtime (CLR)?

Microsoft Visual Basic.Net (version 7) and Microsoft Visual C++.NET (version 7) both support the CTS. Additionally there are versions of a number of other languages that are ported to the CTS. These include Python, COBOL, Perl, Java, and more. Check out the Microsoft Web site for additional languages.

  • + Share This
  • 🔖 Save To Your Account