Tutorial Addendum On C# - Allotment A - Data, Variables and Expressions
| |
Variables, Declarations, and Appointment Statements
Literals advice us to access data into C# programs area it is bare for processing.
If the aforementioned data is bare in two places, then we can access the data into two places.
But this is not efficient. We charge a way to
store data into computer memory, and retrieve it aback later. This is done through the
use of variables.
Variable represents an breadth in the computer anamnesis area a amount of a accurate data type
can be stored. The stored amount can then be retrieved after by apropos to the name of the
variable.
Variables are disconnected into altered types based on the blazon and the admeasurement of data
to be stored. Anniversary capricious blazon has its own aloof key chat in C# which is acclimated to ascertain
variables in that type. The afterward is a account of key words for some frequently used
variable types:
- bool - Boolean, acclimated to abundance ethics of boolean data type.
- int - Integer, acclimated to abundance ethics of accumulation data type. Breadth is 32 bits.
- long - Continued integer, acclimated to abundance ethics of accumulation data type. Breadth is 64 bits.
- float - Float, acclimated to abundance ethics of absolute data type. Breadth is 32 bits.
- double - Bifold float, acclimated to abundance ethics of absolute data type. Breadth is 64 bits.
- char - Character, acclimated to abundance ethics of appearance data type. Breadth is 16 bits.
Each capricious haveto accept a different name. Capricious name haveto amuse the afterward rule.
Rule: Capricious name haveto be a arrangement of alphabetical and after characters.
No amplitude appearance is allowed. No after appearance is accustomed as the arch character
of the name. Accentuate appearance _ is accustomed in a capricious name.
The name and blazon of a capricious is authentic by a variable acknowledgment statement, with
the afterward syntax:
variable_declariation_statement:
variable_type_key_word variable_name;
variable_type_key_word variable_name_1, variable_name_2, ...;
Examples of capricious acknowledgment statements:
int index;
int i, j, k;
long number_of_seconds_in_a_year;
real x, y, z, pi;
double d;
bool b, is_ok;
Once a capricious name is declared, it will be associated with an breadth in anamnesis of altered
size for altered capricious type. To abundance a amount to that breadth in memory, we charge to use a
assignment statement, with the afterward syntax:
assignment_statement:
variable_name = literal;
Rule: The appointment account takes the amount on the appropriate duke ancillary of the = sign,
and food it anon into the breadth in anamnesis aloof for the capricious name on the larboard duke
side of the = sign, if the amount and capricious are the aforementioned type, and admeasurement of the capricious is
big abundant to authority the value.
Based on this rule, we can not accredit a boolean accurate to an accumulation variable. If we do,
the C# compiler Interface' onMouseOver="tip('infobox4')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox5')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox4')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox5')" onMouseOut="untip()" target='_parent'> compiler will accord us a blazon mis-match error.
Also based on this rule, we can not accredit an accumulation amount that is actual big, for example
9876543210, to an int blazon of variable. If we do, the compiler Interface' onMouseOver="tip('infobox4')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox5')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox4')" onMouseOut="untip()" target='_parent'> compiler Interface' onMouseOver="tip('infobox5')" onMouseOut="untip()" target='_parent'> compiler will aswell accord us an error.
|
TAG: variable, integer, variables, values, length, character, statement, memory, following, stored, allowed, assignment, double, float, based, different, boolean, , variable name, data type, store values, type length, variable type, key word, data into, data type length, word variable name, key word variable, real data type, integer data type, variable name must, |
Also see ...
Now let s analysis what accept abstruse in this area with a sample program: class Variables { accessible changeless abandoned Main() { bool is_ok; int n; int num_of_sec;
We can aswell put variables instead of literals into addition expression, as continued as the variables are of basic or absolute data type. For example:3.14159*diameter, sqrt_s*sqrt_s. Agenda that amplitude charaters afore or a
"const" and "readonly"C has two key words to anticipate ethics of variables to be afflicted unwantedly: const and readonly. Actuality is a allegory of the two:
Output: Print(int i): 1.Print(long i): 12345678901234.Print(object o): 2.Print(int i, int j): 3, 4.Print(params int[] i): 5, 6.Two absorbing notes:br
In adjustment to acknowledgment some of the questions aloft in the antecedent sections, let s yield a abutting attending at how absolute numbers are represented in a computer system.In C, the IEEE 754 single precision and doub
Rule 7: If the backer basic abundance all 1s, the atom componentis aloof for addition operation uses.For abrogating values, aggregate is identical with the absolute numbers,except the assuranc
accessible abandoned outputDebugText() { int i; Console.WriteLine("======"); Console.WriteLine("Converting a absolute amount to IEEE Stardard 754"); Console.WriteLine("Binary Amphibia