Sei sulla pagina 1di 35

Object-Oriented Programming Using C#

Objectives

In this session, you will learn to:


Use static variables and static functions
Identify the components of the .NET Framework
Use the Visual Studio .NET IDE

Ver. 1.0 Session 5 Slide 1 of 35


Object-Oriented Programming Using C#
Static Variables and Static
Functions

Each object has its own set of member variables.


To retain the value of a variable throughout the program,
you can declare the variable as a static variable.
To manipulate and use the values of static variables, you
can define a function as static function.

Ver. 1.0 Session 5 Slide 2 of 35


Object-Oriented Programming Using C#
Static Variables

The keyword 'static' means that only one instance of a


given variable exists for a class.
Static variables are used to define constants because their
values can be retrieved by invoking the class without
creating an instance of it.
Static variables can be initialized outside the member
function or class definition.
Unlike other member variables, only one copy of the static
variable exists in the memory for all the objects of that class.

Ver. 1.0 Session 5 Slide 3 of 35


Object-Oriented Programming Using C#
Static Functions

Static functions can access only static variables.


Static functions exist even before the object is created.

Ver. 1.0 Session 5 Slide 4 of 35


Object-Oriented Programming Using C#
Demo: Counting the Number of Times Function is Called by Using Static
Functions

Problem Statement:
John a software developer in Zed Axsis Technology needs to
check how many times a function is called. For the same, he
has been asked to create the function called “CountFunction”.
Help John to create this function.

Ver. 1.0 Session 5 Slide 5 of 35


Object-Oriented Programming Using C#
Introducing the .NET Framework

The .NET Framework, introduced by Microsoft, aims at


integrating various programming languages and services.
The .NET Framework is designed to make significant
improvements in code reuse, code specialization, resource
management, multilanguage development, security,
deployment, and administration.
The .NET Framework consists of all the technologies that
help in creating and running robust, scalable, and
distributed applications.

Ver. 1.0 Session 5 Slide 6 of 35


Object-Oriented Programming Using C#
Introducing the .NET Framework (Contd.)

The .NET suite consists of:


.NET Products
.NET Services
.NET Framework
Let us understand each component of the .NET suite in
detail.

Ver. 1.0 Session 5 Slide 7 of 35


Object-Oriented Programming Using C#
Introducing the .NET Framework (Contd.)

.NET Products:
.NET products aim at allowing developers to create
applications, which are capable of interacting seamlessly with
each other.
All .NET products use eXtensible Markup Language (XML) for
describing and exchanging data between applications.
An example of a .NET product is Visual Studio .NET.

Ver. 1.0 Session 5 Slide 8 of 35


Object-Oriented Programming Using C#
Introducing the .NET Framework (Contd.)

.NET Services:
.NET helps you to create software as Web services. A Web
Service is an application or business logic that is accessible
through standard Internet protocols such as Hypertext Transfer
Protocol (HTTP) and Simple Object Access Protocol (SOAP).
You can identify the service by a Uniform Resource Locator
(URL).
Microsoft has come up with its own set of Web services, known
as My Services.
This service allows users to access data by linking calendars,
phonebooks, address books, and personal references to the
passport authentication service.

Ver. 1.0 Session 5 Slide 9 of 35


Object-Oriented Programming Using C#
Introducing the .NET Framework (Contd.)

The .NET Framework:


It is the foundation for designing, developing, and deploying
applications.
It is the core of the .NET infrastructure because it exists as a
layer between the .NET applications and the underlying
operating system.
Let us understand the .NET Framework in detail.

Ver. 1.0 Session 5 Slide 10 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework

The various components of the .NET Framework are:


Common Language Runtime
The .NET Framework Class Library
User and Program Interfaces

Ver. 1.0 Session 5 Slide 11 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

The following figure displays the various components of


the .NET Framework.

Common Language Runtime (CLR)

.NET Framework Class Libraries

Windows Console Web Forms and


Forms Applications Web Services

Ver. 1.0 Session 5 Slide 12 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

Common Language Runtime (CLR):


CLR is the environment where all programs in .NET are
executed.
CLR provides services such as code compilation, memory
allocation, and garbage collection.
CLR allows the execution of code across different platforms by
translating code into Intermediate Language (IL).
IL is a low level language that the CLR understands.
IL is converted into machine language during execution by the
JIT compiler. During JIT compilation, code is also checked for
type safety.
Type safety ensures that objects are always accessed in a
compatible way.

Ver. 1.0 Session 5 Slide 13 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

CLR consists of a set of common rules followed by all the


languages of the .NET Framework. This set of rules is known
as Common Language Specification (CLS).
CLS enables an object or application to interact with the
objects or applications of other languages.
The classes that follow the rules specified by CLS are termed
as CLS-compliant classes. The classes defined in the .NET
Framework class library are CLS-compliant.
One of the specifications defined in CLS is CTS, which
provides a type system that is common across all languages.
CTS define how data types are declared, used, and managed
in the code at run time.
While executing the program, CLR:
Identifies the process of compilation
Identifies the process of code execution

Ver. 1.0 Session 5 Slide 14 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

The .NET Framework Class Library:


The .NET Framework class library works with any .NET
language, such as VB.NET, VC++ .NET, and VC#.
The .NET Framework class library provides classes that can
be used in the code to accomplish a range of common
programming tasks, such as string management, data
collection, database connectivity, and file access.
The .NET Framework class library comprises of:
Namespaces
Assemblies

Ver. 1.0 Session 5 Slide 15 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

User and Program Interfaces:


At the presentation layer, .NET provides three types of user
interfaces:
Windows Forms
Web Forms
Console Applications
.NET provides a program interface, Web Services, to
communicate with remote components.

Ver. 1.0 Session 5 Slide 16 of 35


Object-Oriented Programming Using C#
Components of the .NET Framework (Contd.)

Advantages of the .NET Framework:


Consistent programming model
Multi-platform applications
Multi-language integration
Automatic resource management
Ease of deployment

Ver. 1.0 Session 5 Slide 17 of 35


Object-Oriented Programming Using C#
Using Visual Studio .NET IDE

The Visual Studio .NET IDE provides you with a common


interface for developing various kinds of projects for the
.NET Framework.
The IDE also provides you with a centralized location for
designing the user interface for an application, writing code,
and compiling and debugging the application.

Ver. 1.0 Session 5 Slide 18 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions

In Visual Studio .NET, an application can be made up of


one or more items, such as files and folders.
To organize these items efficiently, Visual Studio .NET has
provided two types of containers:
Project: It typically contains items that make up the
application. These items are interrelated.
Solution: It usually acts as a container for one or more
projects.

Ver. 1.0 Session 5 Slide 19 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The following figure shows a solution with multiple projects.


Solution Project 1
Items
Project 1

Project 2
Project 2
Items

Miscellaneous
Files

Ver. 1.0 Session 5 Slide 20 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

To create a console application in Visual Studio, you need


to create a project. To create a project, you need to perform
the following steps:
1. Select Start  All Programs  Microsoft Visual Studio
2005  Microsoft Visual Studio 2005. The
Start Page - Microsoft Visual Studio window will be
displayed.

Ver. 1.0 Session 5 Slide 21 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

2. Select FileNewProject. The New Project dialog box will


be displayed.

Ver. 1.0 Session 5 Slide 22 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

3. In the New Project dialog box, select Visual C# from the


Project Types pane and Console Application from the
Templates pane.
4. Specify the name of the application in the Name text box.
5. Specify the location where the new project is to be created in
the Location combo box. You can use the Browse button to
browse to the folder in which the new project is to be created.
6. Click the OK button.

Ver. 1.0 Session 5 Slide 23 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

User Interface Elements of Visual Studio .NET IDE:


When you work with a console application project in Visual
Studio .NET, you can use the following main elements in
Visual Studio .NET IDE.

Ver. 1.0 Session 5 Slide 24 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

Standard Toolbar:
The Standard toolbar is located below the menu bar. It provides
shortcuts for menu commands.

Ver. 1.0 Session 5 Slide 25 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The following table shows the various components of


Standard toolbar.

Name Button Function


NewProject Creates a new project
Add New Item Add new item in the project

Save Saves the Program.

SaveAll Save all the unsaved items


of the application
Cut Places selected text or
objects on the Windows
Clipboard

Copy Places a copy of selected


text or objects on the
Windows Clipboard

Paste Pastes the contents of the


Clipboard on the document.

Start Debugging Compiles and Executes the


current project.

Ver. 1.0 Session 5 Slide 26 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Start Page:


When you start Visual Studio .NET, it displays the
Start Page- Microsoft Visual Studio.
The Start Page is the default home page for the browser provided
within the Visual Studio .NET IDE.
This tab displays some of the recent projects and the last date of
their modification. You can open any one of these projects.

Ver. 1.0 Session 5 Slide 27 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Solution Explorer Window:


The Solution Explorer window lists the solution name, the project
name, and all the classes that are used in the project.

Ver. 1.0 Session 5 Slide 28 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Output Window:


The Output window displays messages for the status of various
features provided in the Visual Studio .NET IDE.

Ver. 1.0 Session 5 Slide 29 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Error List Window:


The Error List window displays a list of errors along with the
source (the file and the line number) of the error. It helps you
identify and locate problems that are detected automatically as
you edit or compile code.

Ver. 1.0 Session 5 Slide 30 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Class View Window:


The Class View window displays the classes, methods, and
properties associated with a particular file. They are displayed in a
hierarchical tree-view depicting the containership of these items.

Ver. 1.0 Session 5 Slide 31 of 35


Object-Oriented Programming Using C#
Creating Projects and Solutions (Contd.)

The Code Editor:


The code editor allows you to enter and edit code. You may use
this editor to add code for your class.

Ver. 1.0 Session 5 Slide 32 of 35


Object-Oriented Programming Using C#
Compiling and Executing Project

To compile and execute the application, you need to


perform the following steps:
1. Select BuildBuild Solution or press F6 to compile the
application.
2. Select DebugStart Debugging or press F5 to execute the
application.

Ver. 1.0 Session 5 Slide 33 of 35


Object-Oriented Programming Using C#
Summary

In this session, you learned that:


The static variable retains its value even after the function to
which it belongs has been executed.
The static functions can access only static variables.
Non-static variables cannot be accessed by using static
functions.
The .NET Framework is made up of many components, such
as Common Language Specification (CLS), Common language
Runtime (CLR), and Just-In-Time (JIT) compiler.
CLS is a set of rules that are followed by all the languages of
the .NET Framework.
When a program is compiled using Visual Studio .NET the
compiler translates the code into the Intermediate Language
(IL) instead of machine language.

Ver. 1.0 Session 5 Slide 34 of 35


Object-Oriented Programming Using C#
Summary (Contd.)

The Just-In-Time (JIT) compiler is used to translate code from


IL into machine language.
The Common Language Runtime (CLR) is the environment
where all .NET applications are executed.
The Visual Studio .NET Integrated Development Environment
(IDE) provides you with a common interface for developing
various kinds of applications for the .NET Framework.
Visual Studio .NET provides two types of containers, projects
and solutions to organize the constituents of an application.

Ver. 1.0 Session 5 Slide 35 of 35

Potrebbero piacerti anche