Chapter-1: Subject: Ite 4131 - Integrative Programming and Technologies 4 Year, Bsc. in Information Technology

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 75

Dr.

Faiz Akram (Assistant Professor)


Subject: ITec 4131 –Integrative Programming and Technologies
4th Year, BSc. in Information Technology
1

CHAPTER- 1
INTRODUCTION

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia


The Evolution of Microsoft .NET:

Dot NET too was not developed overnight.NET evolved over a


period of years during which many technologies came and were
found lacking. So let us have a glimpse at the road map that led to
the development of .NET.

Microsoft’s MS-DOS operating system was initially developed for


a very limited computer (in terms of processing capabilities) and
therefore was designed to run only one application at a time.
Intel Corporation developed more and more powerful CPUs, such
As the 80286 and 80386.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia


continues….
3

Possibilities of running multiple applications concurrently and this


vision led to the development of the cooperatively multitasking
Microsoft Windows environment. Further, it lead to the development
of various technologies like OLE (Object Linking and Embedding),
ODBC (Open Database Connectivity) and ADO (ActiveX Data
Objects) for data exchange, which ultimately led to one of the most
successful component architectures named COM (Component Object
Model).
Having achieved tremendous success in the desktop arena, Microsoft
had the vision of making inroads into the Internet world. And this
strategy had to be developed with web-centric environments in mind.
The .NET strategy is nothing but Microsoft’s vision of future’s web-
centric environments where the focus shifts from the desktop to the
Internet.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues…..
4

Faculty Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues…..
5

.NET is the latest vision about how people will arrange and display
data from different sources distributed over the Internet. In a way
this makes sense because the emphasis is on a user-centric approach.
Simply put, .NET is Microsoft's strategy for delivering software as a
service.

Faculty Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Main differences between .NET 1.1, 2.0, 3.0 and 3.5
6

Below figure '.NET growth' shows how the framework has evolved.
In .NET 1.1 we had the basic framework, web services, CLR, ADO
etc. .NET 2.0 They had modules like MARS, Generics, Partial
classes, DPAPI etc, Dot NET 3.0 had modules like WCF, WPF and
WWF, In .NET 3.5 has new query capabilities like LINQ, AJAX

Figure 2: - .NET Growth

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
7

Each .NET version is released with some significant improvements:


 .NET 1.0 - 2002 Initial version (1st release)
 .NET 1.1 - 2003 Update on 1.0, added provider for Oracle
 .NET 2.0 - 2005 Generics were introduced.
 .NET 3.0 - 2006 WPF, WCF, WF were introduced
 .NET3.5 - 2007  LINQ and ADO .NET Entity Framework
 .NET 4.0 - 2010 Dynamic support for languages and TPL (dynamic
keyword introduced along with Task Parallel library )
 .NET 4.5 - 2012 Asynchronous programming support (async and await
keyword introduced).
.NET 4.5.1-2013 Automatic binding redirection for assemblies.  Ability to
collect diagnostics information to help developers improve the performance
of server and cloud applications. Resizing in Windows Forms controls. You
can use the system DPI setting to resize components of controls. 
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
8

.NET 4.5.2-2013
.NET 4.6-2015 MVC, Web API, and Web Pages are unified into a single
framework called MVC 6.
.NET 4.6.1-2017
.NET 4.6.2
.NET 4.7
.NET 4.7.1
.NET 4.7.2-2019
.NET 4.8
.NET Framework 4.8 is the last version of .NET Framework. .NET
Framework is serviced monthly with security and reliability bug fixes. .NET
Framework will continue to be included with Windows, with no plans to
remove it. You don't need to migrate your .NET Framework apps, but for
new development, use .NET 5 or later.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
BASIC .NET FRAMEWORK:

CLR (Common Language Runtime):


Full form of CLR is Common Language Runtime and it forms the
heart of the .NET framework. All Languages have runtime and it is
the responsibility of the runtime to take care of the code execution
of the program. For example, VC++ has MSCRT40.DLL, VB6 has
MSVBVM60.DLL, and Java has Java Virtual Machine etc.
Similarly, .NET has CLR. Following are the responsibilities of
CLR.
Garbage Collection: - CLR automatically manages memory thus
eliminating Memory leaks. When objects are not referred, GC
automatically releases those memories thus providing efficient
memory management.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
CONTINUES….
10

Code Access Security: - CAS grants rights to program depending on


the security configuration of the machine. Example the program has
rights to edit or create a new file but the security configuration of
machine does not allow the program to delete a file.

Code Verification: - This ensures proper code execution and type


safety while the Code runs. It prevents the source code to perform
illegal operation such as accessing invalid memory locations etc.

IL (Intermediate language)-to-native translators and optimizer’s:-


CLR uses JIT, compiles the IL code to machine code, and then
executes.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
11

CTS: In order that two language communicate smoothly CLR has


CTS (Common Type System).Example in VB you have Integer and in
C++ you have long these data types are not compatible so the
interfacing between them is very complicated.
So “Integer” data type in VB6 and “int” data type in C++ will convert
it to System.int32, which is data type of CTS. CLS is subset of CTS.

CLS (Common Language Specification):


This is a subset of the CTS, which all .NET languages are expected to
support. It was always a dream of Microsoft to unite all different
languages in to one umbrella.
guidelines that language should follow so that it can communicate
with other .NET languages in a seamless manner.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
12

IL (Intermediate Language):(IL)Intermediate Language is also known as


MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate
Language).
.Net DLL:
If any program after compilation interact with registry called dll hell
problem. .Net product never directly interact with O/S but it interact with
assembly (i.e. mapped with assembly).So immigration from registry to
assembly is the evolution of .Net framework i.e. perform scalability.

JIT compiler is a part of the runtime execution environment.


Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
13

In Microsoft .NET there are three types of JIT compilers:


• Pre-JIT: - Pre-JIT compiles complete source code into native code
in a single compilation cycle. This is done at the time of deployment
of the application.
• Econo-JIT: - Econo-JIT compiles only those methods that are
called at runtime. However, these compiled methods are removed
when they are not required.
• Normal-JIT: - Normal-JIT compiles only those methods that are
called at runtime. These methods are compiled the first time they are
called, and then they are stored in cache. When the same methods are
called again, the compiled code from cache is used for execution.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Microsoft .NET

14

Microsoft .NET: Microsoft.NET is the umbrella term for the


Microsoft’s strategy to move from a client-centric model to a
network-centric model. The .NET is important to users as it makes
their information accessible across all devices. The .NET framework
is the actual language and execution platform besides providing an
extensive set of class libraries.
The vision of .NET is globally distributed systems that use XML as
the universal glue to allow functions running on different computers
across an organization or across the world to come together in a
single application. The .NET also aims to make traditional business
applications easier to develop and deploy. Figure 3 shows how
applications and services can be interlinked in an Internet-based
environment.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
15

Fig. 3

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
THE .NET EXPERIENCE
16

The .NET experience will have features like:


Personal and integrated experience. .NET experiences are centered
on the user, integrating their data and preferences into a single
application.
Interactions delivered through smart devices. Users
experience .NET through their interactions with smart devices.
Smart devices are Web enabled appliances, such as personal
computers, handheld computers, and smart phones. Imagine you
wanted to adjust the air conditioning in your house while you were
away from home.
Each of the .NET-enabled devices generates a different interface
for the .NET experience. The desktop computer might present a
graphical map and accept mouse input.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
17

.NET experiences are location-independent. Taking advantage of


the strengths of both locally installed and Web-based
applications, .NET applications can be accessed both online and
offline.

.NET experiences are user-centric. When you are turning on the


heat, the .NET application already knows where you live, based on
your login ID if you log through a website. You do not need to enter
an address. 

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
The .NET Platform
18

.NET as an amalgam of a set of services, specifications, guidelines and tools


for incorporating the .NET vision. It can be further categorized as
composition of the following constituents:
.NET Framework: The .NET Framework is an environment for building,
deploying, and running Web Services and other applications. It consists of
three main parts:
Common Language Runtime – The execution environment for .NET
applications. The Common Language Runtime is a requirement for any
application developed with the framework.
Framework classes-These APIs are provided as part of .NET. The .NET
Framework includes classes, interfaces, and value types that help expedite
and optimize the development process and give you access to system
functionality.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Akram
Continues….
19

ASP.NET – The web technology for the generation of dynamic


web content under the .NET.
.NET Products: This includes Microsoft Windows.NET (with a
core integrated set of building block services), MSN.NET,
personal subscription services, Microsoft Office.NET, Microsoft
Visual Studio.NET, and Microsoft bCentral for .NET.
 
.NET Services: The .NET services will include all the web
services and other corporate services provided by the third party
vendors.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Advantages of .NET
20

The .NET platform will offer the following advantages:


.NET is multi-lingual - WITH THE .NET PLATFORM WE CAN USE
SEVERAL LANGUAGES, SUCH AS C++, JSCRIPT.NET, VB.NET
(ALIAS VB 7) AND C# (A NEW LANGUAGE - PRONOUNCED AS
CSHARP THAT HAS EMERGED WITH .NET).
.NET Applications are portable - Applications compiled as intermediate
code are presented as Portable Executables (PEs). PEs can be implemented
over a vast range of hardware and software architectures.
All languages must comply with a common agreement - For a language
to be eligible for the range of languages supported by the .NET platform, it
must provide a set of possibilities and constructions listed in an agreement
called the Common Language Specification (CLS). To add a language
to .NET all that is required is for it to meet the requirements of the CLS, and
a compiler to convert that language into MSIL.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
21

Managed codes – All code in .NET is managed code because the


runtime provides services that include automatic memory
management, debugging support, enhanced security, and
interoperability with unmanaged code, such as COM components.
Earlier, the developers had to explicitly manage the memory in
languages like C++.

Interoperability – All languages supported by the .NET


framework support a common set of programming classes as their
base class library. All of them support the same set of APIs and
also have same data types as specified by the Common Type
Specification (CTS). This PE is independent of the source code
language and is completely platform-independent.
  of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia
Faculty Presented by Faiz Aram
Continues….
22

Various techniques
According used byframework,
to the .NET technical writers
all tosource
be effective:
code is first converted into
an intermediate code (called IL code) at compilation time. Before this IL
code can be executed, it must be converted to CPU-specific executable
code.

Fig. 4: .NET- Execution flow

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Understanding the .NET Framework:

23

TheVarious
.NET techniques used by technical
Framework writers to be effective:
is an environment for building, deploying,
and running Web Services and other applications. Microsoft .NET
framework is a standard that aims at integrating development of
Web applications and services using open Web standards such as
HTTP and XML. It consists of three main parts:
•The Common Language Runtime
•The Framework classes or Unified Programming Classes
•ASP.NET

The .NET Framework provides a fully managed, protected, and


feature-rich application execution environment, simplified
development and deployment, and seamless integration with a wide
variety of languages.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
24

Common Language
Various techniques Runtime
used by (CLR)
technical writers to be effective:
The .NET Framework provides a run-time environment called the
Common Language Runtime, which manages the execution of code
and provides services that make the development process easier.
The Common Language Runtime makes it easy to design
components and applications whose objects interact across
languages.
you can define a class, then, using a different language, derive a
class from your original class or call a method on it. You can also
pass an instance of a class to a method on a class written in a
different language.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
BASIC ARCHITECTURE OF .NET
25
FRAMEWORK
TheVarious
following Fig:used
techniques 5 depicts the basic
by technical writersarchitecture of .NET Framework.
to be effective:

Fig:5
In Fig. 5 let us consider the lowest layer i.e. the Common Language Runtime
(CLR). The Common Language Runtime is the execution engine for .NET
Framework applications.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
26

TheVarious
secondtechniques
layer isused
thatbyoftechnical
Unifiedwriters to be effective:
Programming Classes. You do not have to
design all your .NET types from the ground up. The .NET Framework includes
classes, interfaces, and value types that help expedite and optimize the
development process and give you access to system functionality.
The third layer is that of Data and XML (ADO.NET); this is used for data
management and maintenance. These services, commonly named as data services.
The fourth layer is that of ASP.NET, Web Forms and Windows Forms. These are
basically the user and program interfaces.
The upper-most layer includes all the languages supported by the .NET
Framework. A few of the languages are already mentioned in Figure: 5. At
present, the .NET Framework supports almost 84 languages.

All the five layers, as shown in Figure: 5, are fully supported in the Visual
Studio.NET IDE provided by Microsoft.
 
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Common Language Specification:
27

AnVarious techniques
int uses 2 bytesused by technical
in C++ whilewriters to 4
it uses bebytes
effective:
in Java. This may lead to
serious problems in multiple language domains. CLS has been designed as
solution for this problem. Refer to Figure: 6

Fig:6

CLS is about language interoperability, its rules apply only to “externally visible” items.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
28

The root namespace for the types in the .NET Framework is the
System namespace.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….

29

The System namespace also contains second-level namespaces.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
C# LANGUAGES AND WEB SUPPORT

30

C# is the language of .NET. Most of the .NET Framework is written using C#


language. While we can program .NET in many languages, C# is specifically
designed to work well with .NET and to support component-based
programming and it offers several key benefits for programmers:
Simplicity
Consistency
Modernity
Garbage collection
Object-Orientation
Type-safety
Scalability
Version support
Flexibility
Let us now see the syntactical portion of C# as described in Table

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Syntactical portion of C#

31

Table1
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Keywords and their purpose in C#

32

Table:2
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
ASP.NET
33

Active Server Pages (ASP) is the server-side technology for creating


dynamic Web pages. ASP.NET offers several important advantages over
previous Web development models:
Enhanced Performance. ASP.NET is compiled managed code running
on the server.
World-class Tool Support. The ASP.NET framework is complimented
by a rich toolbox and designer in the Visual Studio IDE. WYSIWYG
editing, drag-and-drop server controls, and automatic deployment are just
a handful of features this powerful tool provides.
Power and Flexibility. ASP.NET is also language-independent, so you
can choose a language that best applies to your application.
Simplicity. ASP.NET makes it easy to perform common tasks, from
simple form submission and client authentication to deployment and site
configuration.
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
34

Manageability. ASP.NET employs a text-based, hierarchical


configuration system, which simplifies applying settings to your server
environment and web applications.
Customizability and Extensibility. It is possible to extend or replace
any sub-component of the ASP.NET runtime with your own custom-
written component.
Security. With built-in Windows authentication and per-application
configuration, you can be assured that your applications are secure.

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
UNDERSTANDING COMMON LANGUAGE
RUNTIME
35

The runtime is responsible for managing memory allocation, starting


up and killing threads and processes, enforcing security policy. The
MSIL can also be converted efficiently into native code. Before code
can be executed, MSIL must be converted to CPU-specific code by a
Just in Time compiler (JIT compiler).

Fig:7
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Common Language Runtime
36

When a compiler produces MSIL, it also produces metadata, which


describes the types in your code; The MSIL and metadata are
contained in a portable executable (PE).
Just In Time (JIT) Compiler
Before MSIL can be executed, it must be converted by a .NET
Framework Just in Time (JIT) compiler to native code. The CLR
provides three JIT compilers for converting MSIL to native code
Pre JIT
Econo JIT
Normal JIT

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
The Architecture of CLR
37

the CLR creates an environment for code execution called the Virtual Execution
System (VES). Figure 8 explains the precise working a .NET application with
parts of CLR shown as ovals.

Fig: 8
Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
Continues….
38

Common Language Runtime Architecture


First of all the source code is compiled into MSIL then the MSIL is loaded for
being compiled to native code on the fly using one of the JIT compilers and finally
the native code is executed.
Features of CLR
Following are the features that make CLR so unique
Cross Language Integration
Self-Describing Components
Simple Deployment and Versioning
Integrated Security Services
Garbage Collection
High Performance
Versioning
Reusability
Debugging

Faculty of Computing, JIT, Jimma University, Jimma, Oromia, Ethiopia Presented by Faiz Aram
 
Virtual, Override and new in C#:

38

Virtual keyword allows class member to override in derived class.


Suppose I have class A which contains Print method as virtual
method and I have another class B which is derived from class A
which overrides Print method. Following is a code for that.
public class A
{
public virtual void Print()
{
System.Console.WriteLine("Virtual Print method from a");
}
}
 
Continues….

39

public class B:A{


public override void Print() {
System.Console.WriteLine("Override Print method from b");
}}
Now If I create a object of class A like following and run the code Print method
run as normal method. Following is code for that.
class Program{
static void Main(string[] args){
A a=new A();
a.Print();
}}
Once you run this it will have following output.
 
Continues….

40

But when you create a object of class B like following it will completely override the
functionality of class A. That is called method Overriding. Following is a code for that.
class Program
{
static void Main(string[] args)
{
B b=new B();
b.Print();
}
}
Now, if you run this code it will completely override the code and following will be output
as expected.
 
Continues….

41

How to call base class method with override:


In some scenario, we need base class functionality also while overriding the method of
that class. C# provides that functionality with ‘base’ keyword. For that I have changed
code of class B like following.
public class B:A
{
public override void Print()
{
System.Console.WriteLine("Override Print method from b");
base.Print();
}}
Continues….

42

class Program{
static void Main(string[] args)
{
B b=new B();
b.Print();
}}
Now if you run the code you will see output of both method as expected.
Continues….

43

What is difference between new and override in C#?


After looking into above example some people will argue that this functionality
will be achieved by new keyword also. This is call Method hiding where with
new keyword you can hide functionality of base class. Both are slightly
different, simple example to show difference following is a code for that.
namespace Oops
{
class Program{
static void Main(string[] args){
A a=new B();
B b=new B();
a.Print();
b.Print();
Continues….

44
public class A{
public virtual void Print()
{
System.Console.WriteLine("Virtual Print method from a");
}}
public class B:A
{
public override void Print()
{
System.Console.WriteLine("Override Print method from b");
}}}
In this code, same A class Print method is override by B class Print method and I have
created two object of class B. One with reference to base class A and another B itself.
Now let’s run this example and following is a output.
Continues….

45

Here you see both time it will override class A’s Print Method. Now let’s change
above code with new keyword like following.
namespace Oops
{
class Program
{
static void Main(string[] args)
{
A a=new B();
B b=new B();
Continues….

46
a.Print();
b.Print();
}}
public class A{
public virtual void Print()
{
System.Console.WriteLine("Virtual Print method from a");
}}
public class B:A{
public new void Print()
{
System.Console.WriteLine("Override Print method from b");
}}}
 
Continues....

47

Now you run this code .Following is output.

If you see both outputs carefully then you will notice a difference. With
override keyword it will override Print method in both scenarios. While with
new keyword it will only hide print method and both method exist as separate
method so if you create a class A object with B then it will class A’s Print
method without hiding it and If you create class B object with B then it will
hide the base class method and print from class B’s method.
So that was the difference.
 
Programming C#
47

Microsoft developed C#( pronounced c sharp), a new programming language


based on the C and C++ languages. Microsoft describes C# in this way: ”C# is a
simple, modern, object–oriented, and typesafe programming language derived
from C and C++.

C# (pronounced "See Sharp") is a modern, object-oriented, and type-safe


programming language. C# enables developers to build many types of secure
and robust applications that run in . NET. C# has its roots in the C family of
languages and will be immediately familiar to C, C++, Java, and JavaScript
programmers.
 
In c# everything is an object. In other words, all types in C# are objects. C#
properties are similar to Visual Basic language properties. The Rapid Application
Development (RAD) goal in C# is assisted by C#’s use of concepts and
keyword, such as class, structure, statement, operator, and enumeration.
Attributes are another nice feature of C# language.
NOTE:C# is a case sensitive language.
 
C# Language Features

47

Primary characteristics of C# language.


Modern and Object Oriented
Simple and Flexible
Typesafety
Automatic Memory Management
Versioning Control
Cross Platform Interoperability
 
C# Editors and IDEs

47

Before starting your first C# application, you should take a look at


the C# editors available for creating applications. Visual
Studio .NET (VS.NET) Integrated Development Environment
(IDE) is currently the best tool for developing C# applications.
Installing VS .NET also installs the C# command-line compiler
that comes with the .NET Software Development Kit (SDK).
 
If you don’t have VS.NET, you can install the C# command-line
compiler by installing the .NET SDK. After installing the .NET
SDK, you can use any C# editor.
You can use a text editor, such as Notepad or Word pad.
“Hello, C# Word!”
47

Before starting with the C# programming, you must install the C# compiler.
The C# command-line compiler, csc.exe, comes with Microsoft’s .NET SDK.
save the file as first.cs.
Listing 1. “Hello, C# world!” code
using System;
class Hello
{
Static void Main()
{
Console.WriteLine("Hello, C# world!");
}}
You can compile C# code from the command line using this syntax:
csc C:\\temp\first.cs
After compiling your code, the C# compiler creates an .exe file called first.exe
under the current directory. Now you can execute the .exe from window
explorer or from the command line. Figure 1 shows the output.
 
 
Continues....

47

The first line of your program is this:


using System;
The .NET framework class library is referenced in namespaces. The System
namespace contains the Console class, which reads from or writes to the
console.
 The class keyword defines a new class that is followed by a class name, as
seen in the second line of the “Hello, C# World!” code listing:
class Hello
{
...
}
 
Continues....

47

The next line of code is the static void Main() function:


Static void Main() {
Console.WriteLine ("Hello, C# World!");
}
}
In C#, every application must have a static Main() or int Main() entry point. The
concept is similar to that of the Main() function of C++.
The Console class is defined in the System namespace. You can access its class
members by referencing them directly. Writeline(), A method of the Console class,
writes a string and a line terminator to the console.
 
C# Components….Namespace
47

Namespace and Assemblies


The first line of the “Hello, C# World!” program was this:
using System;
This line adds a reference to the System namespace to the program. After
adding a reference to a namespace, you can access any member of the
namespace.
A namespace is simply a grouping of related classes. The root of all
namespaces is the System namespace. If you see namespaces in the .NET
library, each class is defined in a group of similar category. For example, The
System.Data namespace only possesses data-related classes, and System.
Multithreading contains only multithreading classes.

You can access these classes from other application by referencing their
namespaces.
For example, you can create a new namespace MyOtherNamespace with a
method Hello defined in it. The Hello method writes “Hello, C# World!” to the
console. Example of the namespace in listing 2.
Continues….
52
Listing 2: Namespace wrapper for the hello class
// Called namespace
namespace MyOtherNamespace
{
Class MyOtherClass
{
public void Hello()
{
Console.WriteLine ("Hello, C# World!");
}}}
In listing 3, you’ll see how to reference this namespace and call MyOtherClass’s
Hello method from the main program.
Continues….
52

Listing 3: Calling MyOtherNamespace Name space members


using System;
using MyOtherNamespace;
// Caller namespace
namespace HelloWorldNamespace{
class Hello{
static void Main() {
MyOtherClass cls = newMyOtherClass();
  cls.Hello();
}
}
}
Continues….
52

// Called namespace
namespace MyOtherNamespace{
Class MyOtherClass {
Public void Hello()
{
Console.WriteLine("Hello, C# World!");
}
}
}
As you have seen in listing 3, you include a namespace by adding the using
directly. You can also reference a namespace direct without the using directive.
Listing 4 shows you how to use MyOtherClass of MyOtherNamespace.
Continues….
52
Listing 4. Calling the HelloWorld namespace member from the MyOtherNamespace
// Caller namespace
namespace HelloWorldNamespace
{
class Hello
{
static void Main()
{
MyOtherNamespace.MyOtherClass cls =new
MyOtherNamespace.MyOtherClass();
cls.Hello();
}
}
}
Standard Input and Output Streams
52

The System.Console class provides the capability to read streams


from and write streams to the System console. The Read operation
reads data from the console to the standard input stream, and the
Write operation writes data to the standard output stream.
The Write and WriteLine methods write the data to the standard
output stream. Table 1 describes some of the console class
methods. Table 1. The System.Console Class methods.
Continues….
52
Listing 5 shows you how to use the Console class and its members
Listing 5. Console class example
using System;
namespace ConsoleSamp{
class Classs1{
Static void Main(string[ ] args ){
Console.Write("Standard I/O Sample");
Console.WriteLine("");
Console.WriteLine ("= = = = = = = = ");
Console.WriteLine ("Enter your name . . .");
string name = Console.ReadLine();
Console.WriteLine("Output: Your name is : "+ name);
}}}
Continues….
52
Figure2 shows the output of listing 5.

Figure 2. The console class methods output


The Object Class
52
As described, in the .NET framework, all types are represented as objects and are
derived from the Object class. The Object class defines five methods: Equals,
ReferenceEquals, GetHashCode, GetType and ToString. Table 2 describes these
methods, which are available to all types in the .NET library.
Table 2. Object class methods
Delegates
52
Delegates Types
Delegate types are mainly are used with the class events. A delegate type
encapsulates a method with a certain signature, called a callable entity.
Delegates are the typesafe and secure version of function pointers (callback
functionality).
Delegate instances are not aware of the methods they encapsulate; they’re aware
only and return type.
There are three steps in defining and using a delegate: declaration syntax. For
example, this code:
Delegate void MyDelegate()
Declares a delegate named MyDelegate that no arguments and returns void.
The next step is to create an instance of delegate and call it:
MyDelegate del =newMyDelegate(TestMethod);
del();
Continues….
52
Listing 6 shows an example of delegate.
Listing 6.An example of delegate.
delegate void MyDelegate();
class Test{
static void TestMethod()
{
System.Console.WriteLine("Test Method called");
}
static void Main()
{
MyDelegate del = new MyDelegate(TestMethod);
del();
}}
Event Types
52
The event keyword defines an event. An event type enables an object or class to
provide notification of an event from the system. An instance of a delegate type
encapsulates the callable entities. The EventHandler class defines a delegate
definition. For example:
Public delegate void EventHandler(object sender, System.EventArgs e);
Public event EventHandler Click;
Type Conversions
52
C# supports two kinds of type conversions: implicit conversions and explicit
conversions.
 Implicit conversions are conversions in which one type can directly and safely are
converted to another type.
In this conversion, there is no loss of data, as shown in Listing 7.
Listing 7. Conversion example
using System;
class ConversionSamp{
static void Main(){
int num1 = 123;
long num2 = num1;
Console.WriteLine(num1.ToString());
Console.WriteLine(num2.ToString());
}}
Continues….
52
Casting performs explicit conversions. There may be a chance of data loss or even
some errors in explicit conversions.
This is an example of an explicit conversion:
long num1 = Int64.MaxValue;
int num2 =(int)num1;
Console.WriteLine(num1.ToString());
Console.WriteLine(num2.ToString());
The process of converting from a value type to a reference type is called boxing.
Boxing is an implicit conversion. Listing 19 shows an example of boxing.
Listing 8. Boxing example
using System;
class ConversionSamp
{
Continues….
52

static void Main()


{
int num1 = 123;
Object obj = num1;
 Console.WriteLine(num1.ToString());
Console.WriteLine(obj.ToString());
}}
The process of converting from a reference type to a value type is called unboxing.
Listing 9 shows an example of unboxing.
Listing 9. Unboxing example
using System;
class ConversionSamp
{
Continues….
52

static void Main()


{
Object obj = 123;
int num1 = (int)obj;
 
Console.WriteLine(num1.ToString());
Console.WriteLine(obj.ToString());
}
}
Accessibility modifiers
52

A variable can have one or combination of more then one of the following types:
internal, new, private, public, protected, read only, and static.
Accessibility modifiers:

In listing 10, AccessCls is a class accessed by the Main method. The Main method has
access to num1 because it’s defined as a public variable, but not to num2 because it’s a
private variable.
Continues….
52

Listing 10.Variable access modifiers.


using System;
class VarAccess{
class AccessCls{
Public int num1 = 123;
int num2 = 54; }
static void Main(){
AccessCls cls = new AccessCls();
int num1 = 98;
num1 = cls.num1;
//int i = cls. Num2;
Console.WriteLine(num1.ToString());
}}
Continues….
52

When you access class members, the num2 variable is not available in the list of its
members. See figure.

Figure: Available members of AccessCls


Continues….
52

If you try access num2 from the main program, the compiler gives the error shown
in figure
52

THANK YOU

You might also like