University of Makati
College of Computer Science
OOPROGR
Object Oriented Program
MODULE 2
DATA TYPES, OPERATORS
& EXPRESSIONS
Prof. Edgardo Tan Cruz
Prof. Rhonnel S. Paculanan
1
MODULE 2
DATA TYPES, OPERATORS & EXPRESSIONS
INTENDED LEARNING OUTCOMES:
1. Become aware of how C# and .NET evolved and fit together;
2. Learn why C# is being used today for software development;
3. Distinguish between the different types of applications;
4. Explore a program written in C#;
5. Examine the basic elements of a C# program;
6. Compile, run, build, and debug an application;
7. Create an application that displays output; and
8. Work through a programming example that illustrates the lesson’s concepts.
INTRODUCTION
What is Computer Language?
Generally, we use languages like English, Hindi, etc., to make communication between two
persons. That means when we want to make communication between two persons we need a
language through which persons can express their feelings. Similarly, when we want to make
communication between user and computer or between two or more computers we need a
language through which user can give information to the computer and vice-versa. When a user
wants to give any instruction to the computer the user needs a specific language and that
language is known as a computer language.
Computer languages are the languages through which the user can communicate with the
computer by writing program instructions.
Computer Languages Classification
Over the years, computer languages have been evolved from Low-Level to High-Level Languages.
In the earliest days of computers, only Binary Language was used to write programs. The
computer languages are classified as follows:
2
1. Low-Level Language (Machine Language)
Low-Level language is the only language which can be understood by the computer. Binary
Language is an example of a low-level language. Low-level language is also known as
Machine Language. The binary language contains only two symbols 1 & 0. All the
instructions of binary language are written in the form of binary numbers 1's & 0's. A
computer can directly understand the binary language. Machine language is also known as
the Machine Code.
As the CPU directly understands the binary language instructions, it does not require any
translator. CPU directly starts executing the binary language instructions and takes very
less time to execute the instructions as it does not require any translation. Low-level
language is considered as the First Generation Language (1GL).
2. Middle-Level Language (Assembly Language)
Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters. Assembly language is an example of
middle-level language. In assembly language, we use predefined words called mnemonics.
Binary code instructions in low-level language are replaced with mnemonics and operands
in middle-level language. But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into binary language. Assembler is a
translator which takes assembly code as input and produces machine code as output. That
means, the computer cannot understand middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer.
Assembler is used to translate middle-level language into low-level language.
3. High-Level Language
A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules that
are used to make instructions more easily. Every high-level language has a set of
predefined words known as Keywords and a set of rules known as Syntax to create
instructions. The high-level language is easier to understand for the users but the computer
can not understand it. High-level language needs to be converted into the low-level
language to make it understandable by the computer. We use Compiler or interpreter to
convert high-level language to low-level language.
Programs can also be classified by their orientation:
1. Procedural - available instructions are used to create self-contained units called procedures
such as:
ALGOL
BASIC
COBOL
C
Fortran
3
Pascal
2. Object-oriented - reusable objects, containing code and data, are manipulated such as:
C++
Java
Python
Ruby
VB.NET
C#
Evolution of C# and .NET
C# 1.0.NET framework 1.0,1.1 , CLR version 1.0, Visual Studio 2002.
C# 2.0 .NET framework 2.0 , CLR version 2.0, Visual Studio 2005.
C# 3.0 .NET framework 3.0,3.5 , CLR version 2.0, Visual Studio 2008.
C# 4.0 .NET framework 4.0 , CLR version 4.0, Visual Studio 2010.
C# 5.0 .NET framework 4.5 , CLR version 4.0, Visual Studio 2012,2013.
C# 6.0 .NET framework 4.6 , CLR version 4.0 ,Visual Studio 2013,2015.
C# 7.0 .NET framework 4.6,4.6.1,4.6.2 , CLR version 4.0, Visual Studio 2015, 2017 RC.
Overview of .NET Framework
.NET Framework is a technology that supports building and running Windows apps and web
services. .NET Framework is designed to fulfill the following objectives:
Not an operating system;
An environment in which programs run;
4
Resides at a layer between operating system and other applications;
Offers multi-language independence;
One application can be written in more than one language.
Includes over 2,500 reusable types (classes);
Enables creation of dynamic Web pages and Web services; and
Scalable component development.
Figure 1.1 - Visual Studio Integrated Development Environment
Key Language Features
Unified object system
- Everything type is an object, even primitives
Single inheritance
Interfaces
- Specify methods & interfaces, but no implementation
Structs
- A restricted, lightweight (efficient) type
Delegates
- Expressive typesafe function pointer
- Useful for strategy and observer design patterns
Preprocessor directives
Why C#?
One of the newer programming languages;
Conforms closely to C and C++;
Has the rapid graphical user interface (GUI) features of previous versions of Visual Basic;
Has the added power of C++;
Has the object-oriented class libraries similar to Java; and
Can be used to develop a number of applications.
5
- Software components;
- Mobile applications;
- Dynamic Web pages;
- Database access components;
- Windows desktop applications;
- Web services; and
- Console-based applications.
C# Relationship to .NET
Many compilers targeting the .NET platform are available;
C# was used most heavily for development of the .NET Framework class libraries;
C#, in conjunction with the .NET Framework classes, offers an exciting vehicle to
incorporate and use emerging Web standards;
C# is object-oriented; and
In 2001, the European Computer Manufacturers Association (ECMA) General Assembly
ratified C# and its common language infrastructure (CLI) specifications into international
standards.
Types of Applications Developed with C#
Web applications;
Windows graphical user interface (GUI) applications;
Console-based applications; and
Class libraries and stand-alone components (.dlls), smart device applications, and services
can also be created.
Web Applications
ASP.NET is a web application framework developed and marketed by Microsoft to allow
programmers to build dynamic web sites. It allows you to use a full featured programming
language such as C# or VB.NET to build web applications easily.
Figure 1.2 - Web Application written using C#
C# was designed with the Internet applications in mind; and
6
Can quickly build applications that run on the Web with C#.
- Using Web Forms: part of ASP.NET
Windows Applications
Applications designed for the desktop;
Designed for a single platform;
Use classes from System.Windows.Form;
Applications can include menus, pictures, drop-down controls, buttons, text boxes, and
labels; and
Use drag-and-drop feature of Visual Studio.
Figure 1.3 - Windows application written using C#
Console Applications
Normally send requests to the operating system;
Display text on the command console; and
Easiest to create.
- Simplest approach to learning software development; and
- Minimal overhead for input and output of data.
Figure 1.4 – Console application written using C#
Exploring the First C# Program
7
Example 1: First C# Program
Output from the First C# Program
Console-based
application output
Elements of a C# Program
• Comments
✔ line 1 // This is traditionally the first program written;
✔ Like making a note to yourself or readers of your program;
✔ Not considered instructions to the computer;
✔ Not checked for rule violations ; and
✔ Document what the program statements are doing.
Comments
Make the code more readable; and
Three types of commenting syntax.
✔ Inline comments;
✔ Multiline comments; and
✔ XML documentation comments.
Inline Comments
8
Indicated by two forward slashes (//);
Considered a one-line comment;
Everything to the right of the slashes ignored by the compiler; and
Carriage return (Enter) ends the comment.
// This is traditionally the first program written.
Multi-line Comments
Forward slash followed by an asterisk (/*) marks the beginning
Opposite pattern (*/) marks the end
Also called block comments
/* This is the beginning of a block multiline comment. It can go on for several lines or just
be on a single line. No additional symbols are needed after the beginning two characters.
Notice there is no space placed between the two characters. To end the comment, use
the following symbols. */
XML Documentation Comments
Extensible Markup Language (XML);
- Markup language that provides a format for
describing data using tags; and
- Similar to HTML tags.
Three forward slashes (///) mark beginning;
Advanced documentation technique used for XML-style comments; and
Compiler generates XML documentation from them.
Using Directive
Permits use of classes found in specific namespaces without having to qualify them;
Framework class library; and
- Over 2,000 classes included.
Syntax.
- using namespace Identifier.
Namespace
Namespaces provide scope for the names defined within the group;
- Captain example.
Groups semantically related types under a single umbrella;
System: most important and frequently used namespace; and
Can define your own namespace.
- Each namespace enclosed in curly braces: { }
Predefined namespace
(System) – part of .NET 9
From Example 1-1 FCL
Class Definition
• Building block of object-oriented program;
• Everything in C# is designed around a class;
• Every program must have at least one class;
• Classes define a category, or type, of object; and
• Every class is named.
line 1 // This is traditionally the first program written.
line 2 using System;
line 3 namespace HelloWorldProgram
line 4 {
line 5 class HelloWorld User-defined class
line 6 {
line 11 }
line 12 }
• Define class members within curly braces; and
✔ Include data members
10
- Stores values associated with the state of the class
✔ Include method members
- Performs some behavior of the class
• Can call predefined classes’ methods.
✔ Main( )
Main( ) Method
• “Entry point” for all applications;
✔ Where the program begins execution; and
✔ Execution ends after last statement in Main( ).
• Can be placed anywhere inside the class definition;
• Applications must have one Main( ) method; and
• Begins with uppercase character.
Main( ) Method Heading
• line 7 static void Main( )
✔ Begins with the keyword static
✔ Second keyword → return type
- void signifies no value returned
✔ Name of the method
- Main is the name of Main( ) method
✔ Parentheses “( )” used for arguments
- No arguments for Main( ) – empty parentheses
Method Body – Statements
• Enclosed in curly braces;
✔ Example Main( ) method body
line 7 static void Main( )
line 8 {
line 9 Console.WriteLine(“Hello World!”);
line 10 }
• Includes program statements; and
✔ Calls to other method
• Here Main( ) calling WriteLine( ) method.
Method Calls
line 9 Console.WriteLine(“Hello World!”);
• Program statements
• WriteLine( ) → member of the Console class
• Main( ) invoking WriteLine( ) method
• Member of Console class
• Method call ends in semicolon
Program Statements
• Write ( ) → Member of Console class
11
✔ Argument(s) enclosed in double quotes inside ( )
✔ “Hello World!” is the method’s argument
✔ “Hello World!” is string argument
- String of characters
• May be called with or without arguments
✔ Console.WriteLine( );
✔ Console.WriteLine(“WriteLine( ) is a method.”);
✔ Console.Write(“Main( ) is a method.”);
• Read( ) accepts one character from the input device;
• ReadLine( ) accepts string of characters from the input device;
✔ Until the enter key is pressed.
• Write( ) does not automatically advance to next line; and
• Write(“An example\n”).
✔ Same as WriteLine(“An example”); and
✔ Includes special escape sequences.
• Special characters enclosed in double quotes
C# Elements
Figure 1.5 - Relationship among C# elements
Create Console Application
• Begin by opening Visual Studio;
12
• Create new project; and
• Includes special escape sequences.
- Select New Project on the Start page; and
- OR use File → New Project option.
Create New Project
Figure 1.6 - Creating a console application
Code Automatically Generated
Figure 1.7 - Code automatically generated by Visual Studio
Typing Your Program Statements
• IntelliSense feature of the IDE; and
13
• Change the name of the class and the source code filename.
✔ Use the Solution Explorer Window to change the source code filename.
- Select View → Solution Explorer
Rename Source Code Name
Clicking Yes causes
the class name to
also be renamed
Figure 1.8 - Changing the source code name from Program
Compile and Run Application
• To Compile – click Build on the Build menu
• To run or execute application – click Start or Start Without Debugging on the
Debug menu
- Shortcut – if executing code that has not been compiled, automatically
compiles first
• Start option does not hold output screen → output flashes quickly
- Last statement in Main( ), add Console.Read( );
Build Visual Studio Project
14
Figure 1.9 - Execution of an application using Visual Studio
Debugging an Application
• Types of errors
✔ Syntax errors
- Typing error
- Misspelled name
- Forget to end a statement with a semicolon
✔ Run-time errors
- Failing to fully understand the problem
- More difficult to detect
Error Listing
15
REFERENCES:
1. Rajinikanth B. (n.d.) Computer Languages. Retrieved from
http://www.btechsmartclass.com/c_programming/C-Computer-Languages.html
2. Bikesh Srivastava. (2017). History Of C# Programming Language. Retrieved from
https://www.c-sharpcorner.com/blogs/history-of-c-sharp-programming-language
3. Doyle, B. (2015). C# Programming: From Problem Analysis to Program Design, 4th Edition,
Cengage Learning.
16