C# LANGUAGE FUNDAMENTALS
A computer program is the formatting and use of these words in an organized manner, along with a few additional words and symbols. The key parts of a C# language include the following:
- Whitespace
- Keywords
- Literals
- Identifiers
- Comments
- Operators
- Separators
Whitespace :
The blank spaces put into a listing are called whitespace. The basis of this term is that, on white paper, one can never see the spaces. Whitespace can consist of spaces, tabs, line feeds, and carriage returns.
The compiler almost always ignores whitespace. Because of this, one can add as many spaces, tabs, and line feeds as desired.
For example,
int radius = 4;
This is a well-formatted line with a single space between items. This line could have had additional spaces:
int radius = 4 ;
This line with extra spaces executes the same way as the original.
In fact, when the program is run through the C# compiler, the extra whitespace is removed.
One can also format this code across multiple lines:
int
radius
=
4
;
Although this is not very readable, it still works.
If the text is within double quotes. It is written exactly as it is presented between the quotation marks.
Literals :
Literals are straightforward hard-coded values. They are literally what they are!
Literals can be stored by any variable with a type compatible with that of literal.
Identifiers :
Identifiers are used to name classes, methods and variables.Another aspect related to identifiers is CAPITALIZATION STYLE.The capitalization styles used in C# world are :
Pascal Casing : The first letter of each word in the name is capitalized.Recommended for naming classes and methods.
Ex: MyNewBlog.
Camel Casing : Same as Pascal casing but the first word of identifier should be in lower case.Recommend for naming variables.
Ex: myNewBlog.
Separators :
Separators are used to separate various elements in C# from each other.
Ex: Braces - { }, Parentheses - (), Semicolon - ; , comma - , Period or dot operator .
Operators :
Operators are represented by symbols.Operators act on operands.Ex : a+b; '+' is an Operator.
Operators are classified as Binary Operators and Unary Operators.
Binary Operators : It combines two operands to produce the result.Ex : a+b
Unary Operators : They act only upon one operand.Ex : a++
TYPES OF OPERATORS
Assignment Operator :
The assignment operator simply assigns the result of an expression to a variable. In essence the = assignment operator takes two operands. The left hand operand is the variable to which a value is to be assigned and the right hand operand is the value to be assigned. The right hand operand is, more often than not, an expression which performs some type of arithmetic or logical evaluation.
Ex : sum = a + b or x = 10.
Assignment operators may also be chained to assign the same value to multiple variables.
Ex : a = b = c = 100.
Arithmetic Operators :
C# provides a range of operators for the purpose of creating mathematical expressions. These operators primarily fall into the category of binary operators in that they take two operands.
The following table lists the primary C# arithmetic operators:
| Operator | Description |
|---|---|
| -(unary) | Negates the value of a variable or expression |
| * | Multiplication |
| / | Division |
| + | Addition |
| - | Subtraction |
| % | Modulo |
Multiple Operators may be used in a single expression.
Ex : z = a + b - c * d / e.
Operator Precedence :
All operators are not treated equally. There is a concept of "operator precedence" in C#.The following table outlines the C# operator precedence order from highest precedence to lowest:
| Precedence | Operators |
|---|---|
| Highest | + - ! ~ ++x --x (T)x |
| * / % | |
| + - | |
| << >> | |
| < > <= >= is as | |
| == != | |
| & | |
| ^ | |
| | | |
| && | |
| || | |
| :? | |
| Lowest | = *= /= %= += -= <<= >>= &= ^= |= |
NOTE : The assignment operators have the lowest precedence.
Numerous compound assignment operators are available in C#. The most frequently used are outlined in the following table:
| Operator | Description |
|---|---|
| x += y | Add x to y and place result in x |
| x -= y | Subtract y from x and place result in x |
| x *= y | Multiply x by y and place result in x |
| x /= y | Divide x by y and place result in x |
| x %= y | Perform Modulo on x and y and place result in x |
| x &= y | Assign to x the result of logical AND operation on x and y |
| x |= y | Assign to x the result of logical OR operation on x and y |
| x ^= y | Assign to x the result of logical Exclusive OR on x and y |
++ is an Increment Operator and -- is a decrement Operator.
The usage of these operators makes them different like Prefix and Postfix notation.
Prefix Notation :B = ++a.This means increment by 1 first then assign it to B.
Postfix Notation : B = a++.This means first assign then increment by 1.
Comparision Operator :
In addition to mathematical and assignment operators, C# also includes set of logical operators useful for performing comparisons. These operators all return a Boolean (bool) true or false result depending on the result of the comparison.
These operators are binary in that they work with two operands.
Comparison operators are most frequently used in constructing program flow control. The following table lists the full set of C# comparison operators:| Operator | Description |
|---|---|
| x == y | Returns true if x is equal to y |
| x > y | Returns true if x is greater than y |
| x >= y | Returns true if x is greater than or equal to y |
| x < y | Returns true if x is less than y |
| x <= y | Returns true if x is less than or equal to y |
| x != y | Returns true if x is not equal to y |
Logical and Bitwise Operators :
These operators are used for logical and bitwise calculations. Common logical and bitwise operators in C# are:
Operand Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
! Bitwise NOT
&& Logical AND
|| Logical OR
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
! Bitwise NOT
&& Logical AND
|| Logical OR
The operators &, | and ^ are rarely used in usual programming practice. The NOT operator is used to negate a Boolean or bitwise expression.
The Ternary Operator :
C# uses something called a ternary operator to provide a shortcut way of making decisions. The syntax of the ternary operator is as follows:
[condition] ? [true expression] : [false expression]
Ex : Z = x > y ? x : y ;
if x is greater than y then x is returned else, y is returned.
No comments:
Post a Comment