Microsoft Visual C# 2005 Express Edition: Build a Program Now! (4 page)

Read Microsoft Visual C# 2005 Express Edition: Build a Program Now! Online

Authors: Patrice Pelland

Tags: #General, #Computers, #C♯ (Computer program language), #Programming Languages, #C#, #Microsoft .NET Framework, #Computer Books: Languages, #Computer Graphics, #Application software, #C# (Computer program language), #Programming, #Microsoft Visual C# .NET, #Microsoft Visual C♯ .NET, #Electronic books, #Game Programming & Design, #Computing: Professional & Programming, #C (Computer program language), #Computers - Languages, #Programming Languages - C#, #Programming & scripting languages: general

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
9.5Mb size Format: txt, pdf, ePub

Figure 1-1

Some examples of where you might have come across a

reference to .NET

The term
.NET
by itself does not mean much. You could ask 10 different people in the industry, and you would get 10 different answers. The term is widely used and with a lot of different meanings. In fact, .NET has been used with a lot of market hype attached to it, a little bit like the term
MP3
. So in reality, when you hear or read
.NET
, you really should be thinking about the .NET Framework.

Here is a formal definition of the .NET Framework:

The .NET Framework is a platform that allows you to develop software applications
and libraries called “managed applications”; it provides you with the compiler and tools to
be able to build, debug, and execute managed applications.

For our purposes, you could say .NET is the platform that gives you everything you need to develop and run managed applications that run on Windows.

2

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter1.indd 2

CSX_Chapter1.indd 2

10/24/05 2:49:24 PM

10/24/05 2:49:24 PM

We say that applications are managed because their execution is managed by the

.NET Framework. In fact, the .NET Framework is
managing
the execution by providing a controlled runtime environment offering a wide variety of services like loading your applications, managing the memory, and finally monitoring and maintaining the security and integrity while the application is executed. Before .NET (and Java), applications were unmanaged because they were not executed by a controlled runtime environment. No

N O T E

other component of the system provided the services .NET offers. The applications had to
Throughout this book, I’ll be using

manage their own services, which sometimes led to erroneous code, security holes, and data
the terms
framework
and
.NET

Framework
as synonyms.

corruption. Because of these problems, applications were tough to maintain and debug. The .NET Framework provides you with a wide variety of tools such as
compilers
,
debuggers
,
programming languages
, an
execution engine
(named CLR – Common Language Runtime), developer tools, and a large number of predefined “building blocks”

libraries. Those libraries are named
FCL (Framework Class Libraries)
. You can think of each

.NET component as a building block in a house, as illustrated in this image. I won’t put you to sleep with all the

definitions for each block of this house, because

we’re going to use or talk about most of them

in our projects; I’ll simply introduce the blocks

as appropriate. Just consider this illustration and

come back to it as needed.

Two notes about this special house are worth

mentioning.

First, look at the beige component on the

right side of the house. It is not part of the .NET

Framework, but it touches the .NET Framework

at all levels. The doorknob on this component

indicates that through this application, you can

develop applications that will allow you touch all

the components of the .NET Framework.

I M P O R T A N T

It’s not necessary to have Microsoft Visual Studio® to

develop .NET applications, but using it offers many

advantages.

Chapter 1: Introducing Microsoft® Visual C#® 2005 Express Edition

3

CSX_Chapter1.indd 3

CSX_Chapter1.indd 3

10/24/05 2:49:24 PM

10/24/05 2:49:24 PM

Second, notice that Common Language Runtime (CLR) is the primary part of the house’s foundation. It’s a crucial part of the foundation because it’s the engine that loads and manages the execution of source code. All other services you need to develop applications are on top of the CLR.

What Is C#?

C# is one of the available programming languages that target the .NET Framework. Like any spoken/written language, C# has syntax rules and a series of valid words you can use to create your applications. C# is a popular choice for beginners because some people find the syntax simpler than the syntax of many other programming languages.

Is C# an Object-Oriented Programming (OOP) Language?

C# is a fully fledged object-oriented programming language. Let’s talk about what this means.

Object-oriented programming (OOP) is a programming style (or programming paradigm). There are other programming paradigms, such as functional or procedural programming. Languages like C, Fortran, and Pascal are all programming paradigms. But these paradigms focus more on the actions while OOP focuses more on the data itself. Applications that use the OOP paradigm are developed using OOP languages (OOPL). The first OOPL were introduced in the 1960s, but they really became popular in the late 1970s. They are widely used today because most people agree that they’re easy to learn, use, debug, and maintain. For instance, OOPL easily represent real world objects. C# is an OOP language as are Visual Basic .NET, C++, Java, SmallTalk, and Lisp. Programmers use OOP to write programs that represent the decomposition of real

M O R E I N F O

world problems into modules. Those modules represent real world objects and are named
With C++ you can develop pro-

classes or types. You can think of an OOP program as a collection of objects interacting with
cedural applications, pure object-

oriented applications, or a mix of

each other. Using OOP, a programmer defines new types to represent real-world objects,
both.

such as a plane, a person, a customer, a dog, or a car. Those types or classes create objects
4

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter1.indd 4

CSX_Chapter1.indd 4

10/24/05 2:49:25 PM

10/24/05 2:49:25 PM

or instances. An object is a unit that represents one instance of the real world. It’s a selfcontained unit because it includes all the data and functionality associated with that object. This means that each object created in an application contains all the information that characterizes it (
data members
) and all the actions (
methods
) that can access or modify that information.

Here is a simple example in C# that defines a person’s class:

1 using

System;

2

3 public

class

Person

4 {

5

//Data members

6

public string Name;

7

public string Address;

8

public string City;

9

public string State;

10

public string ZIP;

11

public string Country;

12

13

// Methods

14

public virtual void Display()

15

{

16

Console.WriteLine(Name);

17

Console.WriteLine(Address);

18

Console.WriteLine(City);

19

Console.WriteLine(State);

20

Console.WriteLine(ZIP);

21

Console.WriteLine(Country);

22

}

23 }

This class includes public data members and a display method to print the object’s content to the console. The virtual keyword means that a new class derived from this class will be able to write its own implementation of the display method.

Chapter 1: Introducing Microsoft® Visual C#® 2005 Express Edition

5

CSX_Chapter1.indd 5

CSX_Chapter1.indd 5

10/24/05 2:49:25 PM

10/24/05 2:49:25 PM

Let’s use a different example to go over these concepts. My dog, Chopin, is an instance of the class Dog and the class Dog is a subclass of the Animal class. Because Chopin is a Dog, he has some behaviors and data that are proper for a dog. But because Dog is also an Animal, Chopin also inherits some data and behaviors from the Animal class. This means that the instance Chopin of the Dog class has data members that characterize him and methods that I can call on that little furry ball. For example, here is the instance information for the Chopin object:

Data

■ Breed: He’s a Maltese.

■ Gender: He’s male.

■ Weight: His weight is 5.5 pounds.

■ Color: He’s white.

■ Name: His name is Chopin Chabispel.

■ Age: He’s 1.5 years old.

Actions

■ He speaks (barks).

■ He eats.

■ He moves.

■ He sleeps.

All of these data items (breed, gender, weight, color, name, and age) and actions (speak, eat, move, and sleep) characterize him, but they can also characterize any other dog, like my neighbor’s dog, Molly. And if you think about it, those items can characterize any animal. This means that the class Dog
inherits
data members and methods from the class Animal. Let’s say you want to develop an application for a veterinary clinic. To cover the cats that come to your clinic, all you must do is create a Cat class that also inherits from the class Animal. Then each class (Cat or Dog) could
override
the functionality from the Animal class as needed. For instance, for the Cat class the speak method would be “meows”

6

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter1.indd 6

CSX_Chapter1.indd 6

10/24/05 2:49:26 PM

10/24/05 2:49:26 PM

instead of barks.

Let’s look at the Person class example again. This time, an Employee class that derives from the Person class is added. The Employee class derives from the Person class using the : (colon punctuation symbol) followed by the Person element. The keyword
override
changes the implementation of the
Display()
method.

52

public class Employee : Person

53

54 {

T I P

55

public int Level;

In this book, you'll notice that

56

public int Salary;

some code listings include line

57

numbers. If a line does not include

a number, it indicates that the

58

public override void Display()

code is a continuation from the

59

{

previous line. Some code lines

60

Console.WriteLine(Name + " is at level " + Level.ToString() + " and has a
can get rather long and must be

salary of : " + Salary.ToString() + "$");

wrapped to be displayed on the

printed page. If you need to type

61

Console.WriteLine("His address is:");

in the code in Visual C#, be sure

62

Console.WriteLine(Address);

to put continued lines on a single

63

Console.WriteLine(City + "," + State + " " + ZIP);
line.

64

Console.WriteLine(Country);

65

}

66 }

In this case, the Employee class inherits from the Person class and therefore gets all the data fields from that base class. Employee class doesn’t have to redefine all fields in its definition because it gets them automatically from Person. So, for the Employee class, you must specify only what is different from an instance of the Person class. For example, an instance of the Employee class would have level and salary whereas not all instances of the Person class would. Plus, the Display method for Employee could add level and salary information to the displayed message when called.

S E E A L S O

This was just a brief introduction to OOP and some of its concepts. C# supports all of
In .NET, all class ultimately derives

these concepts and many more. Throughout this book you’ll see more OOP concepts, and
from the Object class, even when

when you do, I’ll highlight them in a reader aid information box.

it is not specified.

Here’s the complete listing used in this section with the addition of another class: the Customer class.

Chapter 1: Introducing Microsoft® Visual C#® 2005 Express Edition

7

CSX_Chapter1.indd 7

CSX_Chapter1.indd 7

10/24/05 2:49:26 PM

10/24/05 2:49:26 PM

1 using

System;

2

3 public

class

Person

4 {

5

//Data members

6

public string Name;

7

public string Address;

8

public string City;

9

public string State;

10

public string ZIP;

11

public string Country;

12

Other books

What Really Happened by Rielle Hunter
One of the Guys by Ashley Johnson
Claustrophobia by Tracy Ryan
The Servant’s Tale by Margaret Frazer
Wartime Family by Lane, Lizzie
To Hold by Alessandra Torre