Variables Expressions
Variables Expressions
Variables Expressions
There are also certain keywords that have a specialized meaning to the C#
compiler, such as the using and namespace keywords.
If you use one of these by mistake, the compiler complains, however, so don’t
worry about it.
For example, the following variable names are fine:
myBigVar
VAR1
_test
These are not, however:
99BottlesOfBeer
namespace
It’s-All-Over
Using SimpleTypeVariables
static void Main(string[] args)
{
int myInteger;
string myString;
myInteger = 17;
myString = "\"myInteger\" is";
Console.WriteLine("{0} {1}.", myString, myInteger);
Console.ReadKey();
}
Naming Conventions
Currently, two naming conventions are used in the .NET Framework
namespaces:
PascalCase and CamelCase.
The case used in the names indicates their usage.
They both apply to names that comprise multiple words and they
both specify that each word in a name should be in lowercase except
for its first letter, which should be uppercase.
For camelCase terms, there is an additional rule: The first word
should start with a lowercase letter.
The following are camelCase variable names:
age
firstName
timeOfDeath
&= Bitwise AND It performs the Bitwise AND operation on a &= 10 is equals to
Assignment two operands and assigns a result to the a = a & 10
left operand.
>>= Right Shift It moves the left operand bit values to a >>= 2 is equals to
Assignment the right based on the number of a = a >> 2
positions specified by the second
operand.
<<= Left Shift It moves the left operand bit values to a <<= 2 is equals to
Assignment the left based on the number of positions a = a << 2
specified by the second operand.
namespace Assignmentoperator
{
class Program
{
static void Main(string[] args)
{
int x = 20;
x += 10;
Console.WriteLine("Add Assignment: " + x);
x *= 4;
Console.WriteLine("Multiply Assignment: " + x);
x %= 7;
Console.WriteLine("Modulo Assignment: " + x)
x &= 10;
Console.WriteLine("Bitwise And Assignment: " + x);
x ^= 12;
Console.WriteLine("Bitwise XOR Assignment: " + x);
x >>= 3;
Console.WriteLine("Right Shift Assignment: " + x);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Operator Precedence
• When an expression is evaluated, each operator is
processed in sequence, but this doesn’t necessarily mean
evaluating these operators from left to right. As a trivial
example, consider the following:
var1 = var2 + var3;
• Here, the + operator acts before the = operator. There are
other situations where operator precedence isn’t so
obvious, as shown here:
var1 = var2 + var3 * var4;
• The following table shows the order of
precedence for the operators you’ve encountered
so far, whereby operators of equal precedence
(such as * and /) are evaluated from left to right:
namespace operatorprecendence
{
class Program
{
static void Main(string[] args)
{
int x = 20, y = 5, z = 4;
int result = x / y + z;
Console.WriteLine("Result1: "+result);
Console.WriteLine("Result2: "+result2);
Console.ReadLine();
}
}
}
Operators allow processing of primitive data types and
objects. They take as an input one or more operands and
return some value as a result.
int result;
int x = 20, y = 10;
result = (x + y);
Console.WriteLine("Addition Operator: " + result);
result = (x - y);
Console.WriteLine("Subtraction Operator: " + result);
result = (x * y);
Console.WriteLine("Multiplication Operator: "+ result);
result = (x / y);
Console.WriteLine("Division Operator: " + result);
result = (x % y);
Console.WriteLine("Modulo Operator: " + result);
bool result;
int x = 10, y = 20;
result = (x == y);
Console.WriteLine("Equal to Operator: " + result);
result = (x > y);
Console.WriteLine("Greater than Operator: " + result)
result = (x != y);
Console.WriteLine("Not Equal to Operator: " + result);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Unary Operators
Unlike other operators, the unary operators
operates on a single operand.
Logical Operators
Logical (Boolean) operators take Boolean values
and return Boolean result (true or false).
The basic Boolean operators are "AND" (&&), "OR"
(||), "exclusive OR" (^) and logical negation (!).
The following table contains the logical operators in
C# and the operations that they perform:
namespace LogicalOperator
{
class Program
{
static void Main(string[] args)
{
// AND operator
result = (x <= y) && (x > 10);
Console.WriteLine("AND Operator: " + result);
// OR operator
result = (x >= y) || (x < 5);
Console.WriteLine("OR Operator: " + result);
//NOT operator
result = !a;
Console.WriteLine("NOT Operator: " + result);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
The table and the following example show that the logical
"AND" (&&) returns true only when both variables contain
truth.
Logical "OR" (||) returns true when at least one of the
operands is true.
The logical negation operator (!) changes the value of the
argument. For example, if the operand has a value true and
a negation operator is applied, the new value will be false.
The negation operator is a unary operator and it is placed
before the argument.
Exclusive "OR" (^) returns true if only one of the two
operands has the value true. If the two operands have
different values, exclusive "OR" will return the result true, if
they have the same values it will return false.
Using Statement
1. using statement doesn’t in itself give you access to names in
another namespace.
2. Unless the code in a namespace is in some way linked to your
project, by being defined in a source file in the project or being
defined in some other code linked to the project, you won’t have
access to the names contained.
3. In addition, if code containing a namespace is linked to your
project, then you have access to the names contained in that code,
regardless of whether you use using.
4. using simply makes it easier for you to access these names, and it
can shorten otherwise lengthy code to make it more readable.
Code in ConsoleApplication1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
...
}
The four lines that start with the using keyword are used to declare
that the System,
System.Collections.Generic, System.Linq, and System.Text
namespaces will be used in this C# code and should be accessible
from all namespaces in this file without classification.
The System namespace is the root namespace for .NET
Framework applications and contains all the basic functionality
you need for console applications.
The other two namespaces are very often used in console
applications, so they are there just in case.