Jump to: navigation, search

C Sharp Variables and Constants

1,940 bytes added, 19:07, 11 January 2008
The C# Decimal Variable Type
Both the integer and floating point families of C# variable types have some limitations. Integers can only handle whole numbers, resulting in the fractional part of a value being stripped off. Floats, on the other hand, have problems with rounding accuracy. Clearly the best of both worlds is sometimes needed and to address this requirement the ''decimal'' variable type is provided. The ''decimal'' type is a compromise between integers and float variable types in that it can store fractional parts of a value and provide exact values in computations.
The ''decimal'' variable type is cable capable of holding values in the range 10<sup>-28</sup> all the way up to 10<sup>28</sup> with none of the rounding problems associated with floating point variable types. == C# Boolean Variable Type == The C# variable type is declared using the ''bool'' keyword and allows for the storage of ''true'' and ''false'' values. Boolean variables are particularly useful in flow control constructs such as ''if'' and ''while'' statements.  Unlike some other programming languages, C# boolean variables must be assigned either ''true'' or ''false'' and cannot be assigned 1 or 0: <pre>bool loopFinished = false; loopFinished = true;</pre> == C# Character Variable Type == When we talk about ''characters'' we are referring to individual letters and numbers. For example, the letter 'a' is a character, as is the visual representation of the number '1'. Such characters may be stored in a C# ''char'' variable. A ''char'' variable can contain one character and one character only. It is important o be aware that characters is not limited to those in the English alphabet. A character stored in a ''char'' variable can be Chinese, Japanese, Arabic, Cyrillic, Hebrew or any other type of character you care to mention. In addition, characters can be used for counting in loops and even in mathematical expressions. To assign a character to a variable simply surround the character with singe quotes: C# provides a number of special character constants which have a special meaning when displayed. These special constants perform such tasks as displaying tab and new lines in displayed text. The following table lists the characters, all of which are identified by a preceding backslash (\):   <pre>char myLetter = 'a';</pre> == C# String Variables == In the preceding section we looked at storing individual characters in a ''char'' variable. Whilst this works for storing a single letter or number it is of little use for storing entire words or sentences. For this purpose the ''string'' variable type is supported by C#. Variables of type ''string'' can store a string of any number of characters.

Navigation menu