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

56

Part II

HANDLING DATA

 

 

 

------------

object1.Add (25, 50)

-------------

Because the type of parameters passed to the Add() method in the previous statement are integers, the C# compiler calls the first Add() method.

Default Parameters

Methods are useful if you want certain parameters of a method not to be explicitly initialized.These parameters then take default values as specified in the body of the method.This feature is provided by the default parameters of C++. C# does not support default parameters. However, to overcome this problem, you can overload methods.

In the previous example, you can create overloads of the method Add() to pass default values to it. The code for the Add() method in the previous section does not specify any value for either x or y. However, you can modify the code as shown following to pass a default value to the method.

public int Add (int x)

{

int z = x + 100; return z;

}

The previous code always adds 100 to the value of integer x that is passed as a parameter when the Add() method is called by any class.

You have learned about classes and the methods used with classes. However, when you create an instance of a user-defined class, there may be more than one class with the same name. Therefore, to avoid this confusion, C# provides you with namespaces.

Namespaces

Namespaces are containers that are used to logically group similar classes that have related functionality. You can also use namespaces to group similar data types. Therefore, when you refer a data type or a class, their names are automatically

COMPONENTS OF C#

Chapter 3

57

 

 

 

prefixed with the name of the namespace. This helps the compiler to understand which class is being referred in your code.

In C#, you need to declare each class in a namespace. However, if you do not explicitly declare a class in a namespace, C# automatically places the class in the default namespace.The default namespace is automatically created with the same name as that of the project. A namespace in C# can have more than one class.

Declaring Namespaces

C# provides you with several classes that you can use in your program code. Most of these classes are a part of the System namespace. You can also declare namespaces in the program code and then add classes to them. A namespace is declared using the namespace keyword. While declaring a namespace, you do not need to prefix the namespace declaration with an access modifier. All namespaces in C# are implicitly public, as they can be used across all programs.

namespace Employee

{

class Employee

}

You can place the Employee class in the Employee namespace. In addition, you can include other namespaces, classes, structs, enumerations, and interfaces in a namespace. You will learn about structs and enumerations later in this chapter.

C# allows the use of nested namespaces.Therefore, to refer to a namespace within another namespace, you use periods (.) to separate the names of the namespaces. For example,

namespace Employee

{

namespace Salary

{

class Salary

}

}

To refer to the Salary class, you need to refer to it as Employee.Salary.Salary.

using Employee;
Accessing Namespaces

58

Part II

HANDLING DATA

 

 

 

TIP

You can have two classes with the same name in different namespaces. However, a namespace cannot contain two classes with the same name.

Y After declaring a namespace, you can accessLthe namespace with the using direc-

tive. Therefore, to access the namespaceFEmployee, use the following statement:

M Additionally, as you have seenAearlier, C# allows the use of nested namespaces. It

will, however, be tediousEto write the complete name of a class repeatedly in the

code. To simplify this task, you can declare the class with the keyword in the

T using

beginning of the code, such as:

using Employee.Salary;

In this case, each time you use the class Salary, the compiler can discern that the class Salary within the Salary namespace is being referred.

However, there may be cases when two namespaces contain classes with the same name. To refer to these classes, you need to write the full name in the code. To avoid writing full names in such cases, you can create an alias.

Aliases

C# allows you to create aliases of a class or a namespace with the using keyword. Aliases are short names assigned to classes and namespaces. The syntax of an alias is:

using <alias> = <class>

In the previous example, you can create an alias for the class Salary, such as:

using aliasSalary = Employee.Salary

Now, each time you need to refer to the Salary namespace, you can use the alias name, such as:

aliasSalary.Salary.CalculateSalary()

Team-Fly®