Professional Documents
Culture Documents
4-Basics of C#
4-Basics of C#
Lecture 6
1
A C# Program
used to organize your code, use classes from the System namespace
and it is a container for classes
and other namespaces
container for data and methods, which brings functionality to your
program. Every line of code that runs in C# must be inside a class.
2
A C# Program
1. Language Symbols
2. Variables
3. Comments
4. Constants
5. Reserved words
7. User Input
4
Basic Elements of C#
Language Symbols
1. Language Symbols
- Letters and Characters (A- Z) or (a-z)
- Numbers(0-9)
- Special symbols
5
Special Symbols
• Token: the smallest individual unit of a
program written in any language excluding
whitespace or a comment
• C# tokens include special symbols, word
symbols, and identifiers
• Special symbols in C# include:
6
Variables
• Primary difference
– How much storage is needed
– Whether a negative value can be stored
• Includes number of types
– byte & sbyte
– char
– int & uint
– long & ulong
– short & ushort
8
Data Types
General Syntax
type identifier = expression;
10
Floating-Point Types
11
Examples of Floating-Point
Declarations
double extraPerson = 3.50; // extraPerson originally set
// to 3.50
double averageScore = 70.0; // averageScore originally set
// to 70.0
double priceOfTicket; // cost of a movie ticket
float totalAmount = 23.57f; // note the f must be placed after
// the value for float types
* It is mandantory to suffix number with f or F of float type,
otherwise the number is assumed to be of double.
* Common Error Message: “Literal of type double cannot be
implicitly converted to type `float'”
12
Decimal Types
• Monetary data items
• As with the float, must attach the suffix ‘m’ or ‘M’
onto the end of a number to indicate decimal
– Float attach ‘f’ or ‘F’
14
Strings
• Represents a string of Unicode characters
string studentName;
string courseName = "C# Programming";
string twoLines = "Line1\nLine2";
15
Identifiers
Table 5: Valid
identifiers 18
Examples of Invalid Identifiers
20
Variable Definition in C#
• - Syntax for variable definition in C# is:
<data_type> <variable_list>;
- Examples:
int counter;
double interestRate;
char grade;
21
Allocating Memory with Constants and
Variables (cont’d.)
• Variable: memory location whose content
may change during execution
• Syntax to declare a named constant:
22
Variable Initialization in C#
• Variables are initialized (assigned a value) with an equal
sign followed by a constant expression. The general form
of initialization is:
variable_name = value;
23
Variable Initialization in C#
24
Example
namespace VariDbleDefinition
class Program
/” actuaL 1n1t1a11zat::zon */
b = 28 ,
c = a + b;
Console . IJr 1teL1ne ( "a = {8), b = •J}, c = {2}", a, b, c
);
Console.ieadLine();
When the above code is compiled and executed, it produces the following result:
a = lB, b = 20, c = 30
Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by C# (will not be
executed).
27
Constants(con..)
class Oalendar2
class Calendar3
coast double
daysPerMeek =
(double) days /
(double) weeks;
coast double
daysPerMonth =
(double) days /
(double) months;
28
Reserved Words in C#
Table 7:
C# keywords/
reserved words
29
Basic Elements of C# Operators
Operators (Expressions) :
Arithmetic Operators (+, -, *, /, %).
Relational Operators (<, >, <=, >=).
Equality Operators (==, !=).
Logical Operators (!, &&, ||).
Increment and Decrement Operators (++, --).
Assignment Operators (=, +=, -=, *=, /=, %=).
30
Arithmetic Operators, Operator
Precedence, and Expressions
• C# arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus (or remainder) operator
• +, -, *, and / can be used with integral
and floating-point data types
• Use % only with integral data types
31
Arithmetic Operations
• Simplest form of an assignment statement
resultVariable = operand1 operator operand2;
• Readability
– Space before and after everyoperator
33
Mixed Expressions
• Mixed expression:
– Has operands of different data types
– Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
34
Order of Precedence
• Order of operations
– Order in which the calculations are performed
• Example
– answer = 100;
– answer = answer + 50 * 3 / 25 – 2;
50 * 3 = 150
150 / 25 = 6
6– 2= 4
100 + 4 = 104
35
Order of Operations
Table 9:
Operator precedence
• Associatively of operators
– Left
– Right
36
Precedence of arithmetic operators
37
Basic Arithmetic Operations
string result;
string fullName;
string firstName = "Muhammad";
string lastName = "Ahmad";
40
Increment and Decrement Operators
43
Basic Arithmetic Operations
(continued)
46
Increment and Decrement operators
47
Assignment Operators
can be rewritten as
variable operator= expression;
• Other assignment operators
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
x %= 9 (x = x % 9) 46
Assignment Statement
• The assignment statement takes the
form:
int
numberOfMinutes
, count,
minIntValue;
numberOfMinutes = 45;
count = 0;
minIntValue = -214;
50
Examples of Assignment Statements
char firstInitial,
yearInSchool,
punctuation;
enterKey,
lastChar;
firstInitial = 'B';
yearInSchool = '1';
punctuation = '; ';
enterKey = '\n'; // newline escape character
lastChar = '\u005A'; // Unicode character 'Z'
51
Examples of Assignment
Statements (continued)
double accountBalance,
weight;
bool isFinished;
accountBalance = 4783.68;
weight = 1.7E-3; //scientific notation may be used
52
Examples of Assignment
Statements (continued)
decimal amountOwed,
deficitValue;
53
Examples of Assignment Statements
(continued)
55
User Input
User Input
59
Summary
• C# program: collection of functions, one
of which is always called main
• Identifiers consist of letters, digits, and
underscores, and begins with letter or
underscore
• The arithmetic operators in C# are
addition (+), subtraction (-),
multiplication (*), division (/), and
modulus (%)
• Arithmetic expressions are evaluated
using the precedence associativity rules 60
Summary (cont’d.)