Simple types
are value
types that are a subset of the builtin types in Visual C# .NET,
although, in fact, the types are defined as part of the .NET
Framework Class Library (.NET FCL). Simple types are made up of
several numeric types and a bool
type. These
numeric types consist of a decimal type (decimal
),
nine integral types (byte
,
char
, int
,
long
, sbyte
,
short
, uint
,
ulong
, ushort
), and two
floatingpoint types (float
,
double
). Table 11 lists the
simple types and their fully qualified names in the .NET Framework.
Table 11. The simple data types
Fully qualified name 
Reserved C# keyword 
Value range 


 

0 to 255  

128 to 127  

0 to 65535  

79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335  

1.79769313486232e308 to 1.79769313486232e308  

3.402823e38 to 3.402823e38  

32768 to 32767  

0 to 65535  

2,147,483,648 to 2,147,483,647  

0 to 4,294,967,295  

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  

0 to 18,446,744,073,709,551,615 
The C# reserved words for the various data types are simply aliases for the fully qualified type name. Therefore, it does not matter whether you use the type name or the reserved word: the C# compiler will generate identical code.
It should be noted that the following types are not
CLScompliant:
sbyte
, ushort
,
uint
, and ulong
. These types do
not conform to the rules governing CLS types and therefore, they
might not be supported by other .NET languages. This lack of support
might limit or impede the interaction between your C# code and code
written in another CLScompliant language, such as Visual Basic .NET.
You need to compare a fraction with a value
of type double
or float
to
determine whether they are within a close approximation to each
other. Take, for example, the result of comparing the expression 1/6
and the value 0.16666667. These seem to be equivalent, except that
0.16666666 is precise to only 8 places to the right of the decimal
point, and 1/6 is precise to the maximum number of digits to the
right of the decimal point that the data type will hold.
Verify that the difference between the two values is within an acceptable tolerance:
using System; public static bool IsApproximatelyEqualTo(double numerator, double denominator, double dblValue, double epsilon) { double difference = (numerator/denominator)  dblValue; if (Math.Abs(difference) < epsilon) { // This is a good approximation return (true); } else { // This is NOT a good approximation return (false); } }
Replacing the type double
with
float
allows you to determine whether a fraction
and a float
value are approximately equal.
Fractions can be expressed as a numerator over a denominator; however, storing them as a floatingpoint value might be necessary. Storing fractions as floatingpoint values introduces rounding errors that make it difficult to perform comparisons. Expressing the value as a fraction (e.g., 1/6) allows the maximum precision. Expressing the value as a floatingpoint value (e.g., 0.16667) can limit the precision of the value. In this case, the precision depends on the number of digits that the developer decides to use to the right of the decimal point.
You might need a way to determine whether two values are
approximately equal to each other. This comparison is achieved by
defining a value (epsilon
) that is the smallest
positive value, greater than zero, in which the absolute value of the
difference between two values
(numerator
/denominator

dblValue
) must be less than. In other words, by
taking the absolute value of the difference between the fraction and
the floatingpoint value and comparing it to a predetermined value
passed to the epsilon
argument, we can determine
whether the floatingpoint value is a good approximation of the
fraction.
Consider a comparison between the fraction 1/7 and its floatingpoint
value, 0.14285714285714285. The following call to the
IsApproximatelyEqualTo
method indicates that there
are not enough digits to the right of the decimal point in the
floatingpoint value to be a good approximation of the fraction
(there are 6 digits, although 7 are required):
bool Approximate = Class1.IsApproximatelyEqualTo(1, 7, .142857, .0000001); // Approximate == false
Adding another digit of precision to the third parameter of this method now indicates that this more precise number is what we require for a good approximation of the fraction 1/7:
bool Approximate = Class1.IsApproximatelyEqualTo(1, 7, .1428571, .0000001); // Approximate == true
No credit card required