Csharp expressions, types, variables, control statements of both selection and loops, methods, namespaces, classes, inheritance and polymorphism topics are included in this presentation.
2. Agenda
• Expressions, Types, and Variables
• Control Statements - Selection
• Control Statements - Loops
• Methods
• Namespaces
• Introduction to Classes
• Class Inheritance
• Polymorphism
3. Expressions, Types, and Variables
• Variables: storage locations for data
• int num = 428;
• C# is a strongly typed language.
• All operations are based on variable Types
• C# simple types consist of:
• Boolean Type
• 3 numeric type: Integrals, Floating Point, Decimal
• String
4. Expressions, Types, and Variables
• The Boolean Type
• Declared by keyword: bool
• Has two values: true or false
5. Expressions, Types, and Variables
• In C++, a value of type bool can be converted to a value of type int; in
other words
• false is equivalent to zero and true is equivalent to nonzero values.
• In C#, there is no conversion between the bool type and other types.
6. Expressions, Types, and Variables
• Integral Types
• Number, either signed and unsigned and char type
7. Expressions, Types, and Variables
• Floating Point and Decimal Types
• To represent real numbers
8. Expressions, Types, and Variables
• The String Type
• A string is a sequence of text characters.
• “This is a string example”
• Some characters are not printable
9. Expressions, Types, and Variables
• Operands and Operators
• Operand is a piece of information or data
• Operator specifies what action should be done with the operand(s).
• operandOne operator operandTwos
• Expressions
• An expression is simply one or more operands and their associated operator
treated as a single entity.
• answer = operand1 / operand2
10. Expressions, Types, and Variables
• Statements
• In C#, a program statement is one or more expressions terminated by a
semicolon.
• answer = operand 1 / operand2;
13. Control Statements - Selection
• If statements
• An if statement allows you to take different paths of logic, depending on a
given condition.
• There are two forms of OR:
• Regular OR (|) operator will evaluate both sub-expressions every time.
• Conditional OR (||) will evaluate the second sub-expression only if the first sub-
expression evaluates to false.
• There are two forms of AND:
• Regular AND operator will evaluate both expressions every time.
• Conditional AND operator will evaluate the second sub-expression only when the first
sub-expression evaluates to true.
14. Control Statements - Selection
• Switch Statements
• A type of selection control mechanism used to allow the value of a variable or
expression to change the control flow of program execution via a multiway
branch.
• Break!
• Go to!
15. Control Statements - Loops
• Learn the while loop
• A while loop will check a condition and then continues to execute a block of
code as long as the condition evaluates to a boolean value of true.
• Learn the do loop
• A do loop is similar to the while loop, except that it checks its condition at the
end of the loop.
16. Control Statements - Loops
• Learn the for loop
• By using a for loop, you can run a statement or a block of statements
repeatedly until a specified expression evaluates to false.
• Learn the foreach loop
• The foreach statement repeats a group of embedded statements for each
element in an array or an object collection.
17. Methods
• Methods are to split our logic into units
• Name should be meaningful
• Should be associated with the task
• Parameters are to transfer information to and from method
attributes modifiers return-type method-name(parameters)
{
statements
}
19. Namespaces
• Namespaces are C# program elements designed to help you organize
your programs.
• Also to avoid name conflicts
20. Introduction to Classes
• Classes are templates to objects
• Constructors
• Instance and Static methods, again!
• Destructors
• To release the allocated resources
21. Inheritance
• Inheritance is one of the primary concepts of object-oriented
programming. It allows you to reuse existing code.
22. Polymorphism
• It allows you to invoke derived class methods through a base class
reference during run-time.
• Sending the same message, getting the correct response