To Documents

Constants, Variables, and Operators

 

Constants

We consider four types of C# constants: integer, double, string, and boolean.

  1. Integer   Positive, negative, or zero numbers that do not have a fractional part, for example, 34, 643938, -21,
    -38472874, or 0.
     
  2. C# can represent any integer between -231 (-2147483648) and 231 - 1 (2147483647).
     
  3. 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:
     
  4. 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

     
  5. String   A sequence of characters delimited by single or double quotes.
    Examples of string literals:
    abc   g32   *&^(%$#     (space)    (null string)
    
  6. 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:
 
  1. 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:
     
    C# is case sensitive, which means that capitalization matters. The names numberOfCustomers and numberofcustomers represent two different variables.
     
  2. C# tends to use camel casing (first letter lowercase, each letter of a new word in uppercase, no embedded spaces) for local variables.
     
  3. Examples of variables spelled with camel casing: numberOfCustomers, xValue, txtInputTextBox.
     
  4. 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.
     
  5. Examples of variables spelled with Pascal casing: Examples of variables spelled with Pascal casing: TextBox, Random, CustomerAccount, ToString.
     
  6. 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.
     
  7. 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):

 

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:

 

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.