To Documents
Constants, Variables, and Operators
 
Constants
We consider four types of C# constants: integer, double, string, and boolean.
- Integer   Positive, negative, or zero numbers that do not
have a fractional part, for example,
34,
643938,
-21,
-38472874,
or 0.
C# can represent any integer
between -231 (-2147483648) and 231 - 1
(2147483647).
- Double Double (double precision floating point) constants are positive or
negative numbers with a fractional part, for example,
46.1982,
0.00441,
-237.1273. The constant
23.000 also qualifies as a floating
point number, even though it could be converted to an integer without loss of precision.
Floating point numbers can also be written in base 10 scientific notation with positive
or negative exponents. Here are some examples:
C# Constant | Scientific Notation | Meaning |
3e2 |
3 x 102
| 300 |
5.98e24 |
5.98 x 1024
| Mass of Earth in Kilos |
9.11e-31 |
9.11 x 10-31
| Mass of Electron in Kilos |
- String A sequence of characters delimited by single or double quotes.
Examples of string literals:
abc g32 *&^(%$# (space) (null string)
- Boolean A boolean (or logical) literal can take on
either of the values
true or
false.
Variables
A variable is a location in a computer that contains data, which might be changed
during the execution of a C# script. There are three aspects to a variable:
- Name The name of a C# variable must start with a letter (upper or lower
case) and consist entirely of letters or digits. The upper limit to the length of a
C# variable name is 512 characters. However, names longer than about 12 characters
become unwieldy. Here are some examples of C# variable names:
x
x123
customer3
numberOfCustomers.
C# is case sensitive, which means that capitalization matters. The
names
numberOfCustomers
and
numberofcustomers
represent two different variables.
- C# tends to use camel casing (first letter lowercase, each letter of a new
word in uppercase, no embedded spaces) for local variables.
- Examples of variables spelled with camel casing:
numberOfCustomers, xValue, txtInputTextBox.
- C# tends to use Pascal casing (first letter uppercase, each letter of a new
word in uppercase, no embedded spaces) for class and method names.
- Examples of variables spelled with Pascal casing:
Examples of variables spelled with Pascal casing:
TextBox, Random, CustomerAccount, ToString.
- Address The address of a variable is the location in the computer's memory
where the data is stored. The C# interpreter keeps a symbol table that maintains the
correspondence between the variable's name and its address, so the programmer need not be
explicity concerned about the address.
- Value The value of a variable is the data currently stored at its address.
A variable can contain an integer, floating point number, or a string.
Variable declaration is required in C#. Here is how to declare
variables of type int (integer), double (double precision floating point), string (string of
characters) and bool (boolean true or false):
int m, n;
double x, y;
string s, t;
bool f, g;
The Assignment Operator
The assignment operator is used to assign a value to a variable. For example,
assigns the value 5 to the variable x.
Variable Initialization
A variable can be declared and assigned a value in one line. This
is called initialization. For example:
int n = 5;
double x = 3.65;
string s = abc;
bool f = true;
Operators
Here is a table of some common C# operators.
Symbol | Operand 1 | Operand 2 | Meaning |
Precedence |
- |
Number | | Negation | 1 |
* |
Number | Number | Multiplication | 2 |
/ |
Number | Number | Floating Point Division |
2 |
+ |
Number | Number | Addition | 3 |
+ |
String | String | Concatenation | 3 |
+ |
String | Number | Concatenation | 3 |
+ |
Number | String | Concatenation | 3 |
- |
Number | Number | Subtraction | 3 |
< |
Number | Number | Less Than | 4 |
<= |
Number | Number | Less Than Or Equal | 4 |
> |
Number | Number | Greater Than | 4 |
>= |
Number | Number | Greater Than Or Equal | 4 |
== |
Number | Number | Equal To | 4 |
!= |
Number | Number | Not Equal To | 4 |
& |
Number | Number | Logical And |
5 |
| |
Number | Number | Logical Or |
6 |
= |
Variable | Expression | Assignment |
7 |
Note: There is no C# operator for exponentiation. Use the
function Math.pow for exponentiation.