Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

Welcome to the C# Programming Primer:

Sams Publishing and Borland have teamed up to provide you with C# content from popular Sams books, as well as a special coupon offer for Borland customers.

These chapters cover the basics of programming with C# on the .NET platform, along with ASP.NET Web programming and more advanced C# techniques.

For a limited time only, you can purchase any of Sams C# and C#Builder titles for 35% off the cover price. To redeem your coupon, go to:

1)www.informit.com/sales/ad/Borlandoffer

2)Select from the book titles listed below

3)at checkout, type in the coupon code: BORLAND35 to receive your discount

The following titles have been selected for this coupon offer:

C#Builder Kick Start

(New title coming this October! Pre-order with this coupon) 0-672-32589-6, Mayo, 10/8/2003, $34.99

Borland C++ Builder 6

 

Developer's Guide

Sams Teach Yourself C# in 21

$59.99

 

Days

 

 

 

$39.99

 

 

Borland

C# Unleashed

 

 

 

 

JBuilder

 

 

$49.95

 

 

Developer's

 

 

 

 

 

Guide

 

 

 

$59.99

 

 

 

 

 

Sams Teach Yourself the C#

C# Primer Plus

Language in 21 Days

$44.99

$34.99

 

 

 

 

 

Sams Teach

 

 

 

 

 

 

Yourself C# in

 

 

 

24 Hours

 

 

 

$29.99

 

 

 

 

 

 

 

 

 

Contents

Sams Teach Yourself C# in 21 Days

DAY 1 Getting Started with C#

1

DAY 2

Understanding C# Programs

22

DAY 3

Storing Information with Variables

42

DAY 4

Working with Operators

72

DAY 5

Control Statements

102

C# PRIMER PLUS

CHAPTER 3

A GUIDED TOUR THROUGH C#: PART I

130

CHAPTER 4

A GUIDED TOUR THROUGH C#: PART II

161

CHAPTER 5

YOUR FIRST OBJECT-ORIENTED C# PROGRAM

181

C# and the .net framework

CHAPTER 3.2

User Interface Components

235

CHAPTER 3.3

Data Bound Controls

296

CHAPTER 3.4

Windows Forms Example Application (Scribble .NET)

316

CHAPTER 3.5 GDI+: The .NET Graphics Interface

350

CHAPTER 3.6

Practical Windows Forms Applications

394

Sams Teach Yourself C# Web Programming in 21 Days

Day 13

Introducing Web Services

441

Day 14

Publishing Web Services

461

Day 15

Consuming a Web Service

474

Day 16

Putting It All Together with Web Services

499

C# Unleashed

CHAPTER 23

Multi-Threading

520

CHAPTER 25

String Manipulation

526

CHAPTER 28

Reflection

556

CHAPTER 31

Runtime Debugging

570

WEEK 1

DAY 1

Getting Started with C#

Welcome to Sams Teach Yourself C# in 21 Days! In today’s lesson you begin the process of becoming a proficient C# programmer. Today you

Learn why C# is a great programming language to use

Discover the steps in the Program Development Cycle

Understand how to write, compile, and run your first C# program

Explore error messages generated by the compiler and linker

Review the types of applications that can be created with C#

What Is C#?

It would be unusual if you bought this book without knowing what C# is. It would not, however, be unusual if you didn’t know a lot about the language. Released to the public in June 2000, C#—pronounced See Sharp—has not been around for very long.

C# is a new language created by Microsoft and submitted to the ECMA for standardization. This new language was created by a team of people at Microsoft led by Anders Hejlsberg. Interestingly, Hejlsberg is a Microsoft

8

Day 1

Distinguished Engineer who has created other products and languages, including Borland Turbo C++ and Borland Delphi. With C#, he focused on taking what was right about existing languages and adding improvements to make something better.

C# is a powerful and flexible programming language. Like all programming languages, it can be used to create a variety of applications. Your potential with C# is limited only by your imagination. The language does not place constraints on what you can do. C# has already been used for projects as diverse as dynamic Web sites, development tools, and even compilers.

C# was created as an object-oriented programming (OOP) language. Other programming languages include object-oriented features, but very few are fully object-oriented. Later in today’s lesson you will understand how C# compares to some of these other programming languages. You’ll also learn what types of applications can be created. On Day 2, “Understanding C# Programming,” you will learn what it means to use an object-orient- ed language.

Why C#?

Many people believed that there was no need for a new programming language. Java, C++, Perl, Microsoft Visual Basic, and other existing languages were believed to offer all the functionality needed.

C# is a language derived from C and C++, but it was created from the ground up. Microsoft started with what worked in C and C++ and included new features that would make these languages easier to use. Many of these features are very similar to what can be found in Java. Ultimately, Microsoft had a number of objectives when building the language. These objectives can be summarized in the claims Microsoft makes about C#:

C# is simple.

C# is modern.

C# is object-oriented.

In addition to Microsoft’s reasons, there are other reasons to use C#:

C# is powerful and flexible.

C# is a language of few words.

C# is modular.

C# will be popular.

Getting Started with C#

9

Caution

The following section contains a lot of technical terms. Don’t worry about

1

understanding these. Most of them don’t matter to C# programmers! The

 

ones that do matter will be explained later in this book.

 

 

 

 

C# Is Simple

C# removes some of the complexities and pitfalls of languages such as Java and C++, including the removal of macros, templates, multiple inheritance, and virtual base classes. These are all areas that cause either confusion or potential problems for C++ developers. If you are learning C# as your first language, rest assured—these are topics you won’t have to spend time learning!

C# is simple because it is based on C and C++. If you are familiar with C and C++—or even Java—you will find C# very familiar in many aspects. Statements, expressions, operators, and other functions are taken directly from C and C++, but improvements make the language simpler. Some of the improvements include eliminating redundancies. Other areas of improvement include additional syntax changes. For example, C++ has three operators for working with members: ::, ., and ->. Knowing when to use each of these three symbols can be very confusing in C++. In C#, these are all replaced with a single symbol—the “dot” operator. For newer programmers, this and many other features eliminate a lot of confusion.

Note

If you have used Java and you believe it is simple, you will find C# to be simple. Most people don’t believe that Java is simple. C# is, however, easier than Java and C++.

C# Is Modern

What makes a modern language? Features such as exception handling, garbage collection, extensible data types, and code security are features that are expected in a modern language. C# contains all of these. If you are a new programmer, you might be asking what all these complicated-sounding features are. By the end of your twenty-one days, you will understand how they all apply to your C# programming!

Note

Pointers are an integral part of C and C++. They are also the most confusing part of the languages. C# removes much of the complexity and trouble caused by pointers. In C#, automatic garbage collection and type safety are

NEW TERM
NEW TERM

10

Day 1

an integral part of the language. If you are not familiar with the concepts of pointers, garbage collection, and type safety, don’t worry. These are all explained in later lessons.

C# Is Object-Oriented

The keys to an object-oriented language are encapsulation, inheritance, and polymorphism. C# supports all of these. Encapsulation is the placing of functionality

into a single package. Inheritance is a structured way of extending existing code and functionality into new programs and packages. Polymorphism is the capability of adapting to what needs to be done. Detailed explanations of each of these terms and a more detailed description of object orientation are provided in Day 2’s lesson. Additionally, these topics are covered in greater detail throughout this book.

C# Is Powerful and Flexible

As mentioned before, with C# you are limited only by your imagination. The language places no constraints on what can be done. C# can be used for projects as diverse as creating word processors, graphics, spreadsheets, and even compilers for other languages.

C# Is a Language of Few Words

C# is a language that uses a limited number of words. C# contains only a handful of terms, called keywords, which serve as the base on which the language’s func-

tionality is built. Table 1.1 lists the C# keywords. A majority of these keywords are used to describe information. You might think that a language with more keywords would be more powerful. This isn’t true. As you program with C#, you will find that it can be used to do any task.

TABLE 1.1 The C# Keywords

abstract

as

base

bool

break

byte

case

catch

char

checked

class

const

continue

decimal

default

delegate

do

double

else

enum

event

explicit

extern

false

finally

fixed

float

for

foreach

goto

if

implicit

in

int

interface

internal

is

lock

long

namespace

new

null

object

operator

out

override

params

private

protected

public

NEW TERM

Getting Started with C#

11

TABLE 1.1 continued

readonly

ref

return

sbyte

sealed

1

 

short

sizeof

stackalloc

static

string

 

struct

switch

this

throw

true

 

try

typeof

uint

ulong

unchecked

 

unsafe

ushort

using

virtual

void

 

while

 

 

 

 

 

 

 

 

 

 

 

Note

There are a few other words used in C# programs. While not keywords, they should be treated as though they were. Specifically, get, set, and value.

C# Is Modular

C# code can (and should) be written in chunks called classes, which contain routines called member methods. These classes and methods can be reused in other

applications or programs. By passing pieces of information to the classes and methods, you can create useful, reusable code.

Note

On Day 2, you learn about classes, and on Day 6, “Classes,” you learn how to start creating your own.

C# Will Be Popular

C# is one of the newest programming languages. At the time this book was written, it was unknown as to what the popularity of C# would be, but it is a good bet that this will become a very popular language for a number of reasons. One of the key reasons is Microsoft and the promises of .NET.

Microsoft wants C# to be popular. Although a company cannot make a product be popular, it can help. Not long ago, Microsoft suffered the abysmal failure of the Microsoft Bob operating system. Although Microsoft wanted Bob to be popular, it failed.

C# stands a better chance of success than Microsoft Bob. I don’t know whether people at Microsoft actually used Bob in their daily jobs. C#, however, is being used by Microsoft. Many of its products have already had portions rewritten in C#. By using it, Microsoft helps validate the capabilities of C# to meet the needs of programmers.

Microsoft .NET is another reason why C# stands a chance to succeed. .NET is a change in the way the creation and implementation of applications is done. Although virtually