Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

18

Part I

INTRODUCTION TO C#

 

 

 

n this chapter, you will learn about the basics of C#. This chapter will discuss

Ivariables and data type casting. You will also learn about arrays and strings

used in C#. Finally, you will be introduced to the statements and expressions used

in C#.

 

 

 

Y

 

 

 

L

 

 

F

Introduction to C#

 

 

 

 

M

 

C# is an advanced version of C and C++ and is designed specially for the .NET

environment. C#, pronounced C sharp, is a new object-oriented language used by

E

 

 

programmers worldwide to develop applications that run on the .NET platform.

T

 

 

 

However, C# is not a part of the .NET environment. C# is a part of Microsoft

Visual Studio .NET 7.0.

Ahe other languages included in the Visual Studio pack-

age are Visual C++ and Visual Basic. Visual Studio 7.0 also includes scripting languages, such as VBScript and JScript. You can use all these languages to create applications that run in the .NET environment. C# is a significant step in the evolution of programming languages, and C# is an ideal solution for high-level business applications. Using C#, you can create a wide range of projects that can be used to build a complete client/server application.

C# builds on the features of C, C++, Visual Basic (VB), and Java to provide a complete environment for developing applications.C# merges the power of C, the object-oriented features of C++, and the graphical interface of VB. In addition, the programs in both C# and Java compile to a byte code.

Now look at the basic components of a C# application.

Variables

Variables are storage locations for values in C#. A variable has a variable name and a data type associated with it. The data type represents the type of values that can

Team-Fly®

C# BASICS

Chapter 2

19

 

 

 

be stored in the variable. Variables can store characters, character strings, numeric values, or memory addresses.

Initializing Variables

To use a variable, you first need to declare it. In C#, you can declare a variable by using the following syntax:

<modifiers> <data type> <variable1, variable2,..........>;

Here, modifiers are the access modifiers that are used to define the accessibility level of a variable. C# supports five types of access modifiers: public, protected internal, protected, internal, and private. Data type is the type of the variable.

To use a variable in an application, you need to assign a value to it. You can assign a value to a variable by using the assignment operator (=). To assign a value 10 to the integer variable x, you use the following statement:

public int x = 10;

In C#, you cannot use a variable without initializing it. You can assign a welldefined initial value to a variable. Such a variable is called an initially assigned variable. C# also supports an initially unassigned variable that does not have a well-defined initial value assigned to it. However, you need to assign a welldefined value to an initially unassigned variable before using it.

Variable Modifiers

Variable modifiers are used to define the features of a variable. Variable modifiers specify the accessibility levels of a variable. For example, a variable modifier decides whether a variable can be used or modified outside the class in which it is declared. The variable modifiers that are supported by C# are listed in Table 2-1.

20

Part I

 

INTRODUCTION TO C#

 

 

 

 

 

 

Table 2-1 The Variable Modifiers in C#

 

 

 

 

 

 

 

Variable Modifier

Description

 

 

 

 

 

 

 

internal

An internal variable is accessed from the current program in which

 

 

 

 

it is declared.

 

 

private

A private variable is accessed from the type that contains it.

 

 

protected

A protected variable is accessed from the containing class or the

 

 

 

 

types derived from the containing class.

 

 

public

A public variable can be accessed fr om anywhere.

 

 

read-only

A read-only variable is assigned a value when the variable is declared

 

 

 

 

initially. If you do not assign a value to a read-only variable when it is

 

 

 

 

first declared, the variable takes the default value of that type. As the

 

 

 

 

name indicates, you cannot change the value of a read-only variable.

 

 

static

A static variable is accessed directly fr om the class and not from the

 

 

 

 

instance of the class.

 

 

 

 

 

You can specify the variable modifier while declaring a variable.

Variable Data Types

The data type of a variable defines the type of the variable. Figure 2-1 displays the data types in C#.

Types of Variables

There are seven types of variables in C#. These are as follows:

Static variables. A static variable has a static modifier. You can access a static variable directly from the class to which it belongs. You do not need to create an instance of a class to access a static variable. A static variable becomes active when the program in which it is declared is loaded and becomes inactive when the program terminates.

Instance variables. An instance variable is declared without the static modifier.

C# BASICS

Chapter 2

21

 

 

 

FIGURE 2-1 Variable data types

Array elements. An array element stores the starting address of an array in memory. To access an array element, you need to create an instance of the array.

Value parameters. A value parameter is a variable declared without a ref or out modifier. When you call a method that contains the value parameter, the parameter becomes active. It takes the value of the argument that you specify in the method. When the method is returned, the value parameter becomes inactive.

Reference parameters. A reference parameter has a ref modifier. A reference parameter is initialized with the value of the underlying variable. A reference parameter stores the location of the argument that is specified when the method is declared.

Output parameters. An output parameter is a variable declared with the out modifier. The out modifier allows you to pass a variable, which is not initialized, to a method. The variable then takes the value from the method to which it is passed.