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

COMPONENTS OF C#

Chapter 3

59

 

 

 

Here, CalculateSalary() is a method in the Salary class.

You have learned about classes and namespaces. The next section offers some information about structs. Structs are data structures similar to classes and are important components of C#.

Structs

Structs are data structures that contain constructors, constants, variables, methods, indexers, properties, operators, and nested types. However, unlike classes, which are reference types, structs are value types.This implies that structs do not require allocation of heap. In addition, a variable declared of the type struct directly contains data, in contrast to a class variable that contains only a reference to the data.

Similar to declaring a class, you can also declare a struct in C#. Structs are declared using the struct keyword.

<modifier> struct <struct name>

{

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

}

Modifiers used with structs are similar to those used with classes. Struct modifiers include new, public, private, protected, and internal. However, you cannot use the abstract and sealed modifiers with structs. Structs are implicitly sealed, as they do not support inheritance.

Structs are used to group similar data. This data can then be easily copied from one struct to another. Consider the following example:

public struct Employee

{

public int Empid; public string Empname; public string Empemail; public string Empsalary;

}

60

Part II

HANDLING DATA

 

 

 

The previous struct includes variables that are used to store employee information. Once a struct is declared, the variables in the struct are initialized to default values. However, to copy these values from one struct to another, you need first to initialize the struct with the new keyword. The new keyword is used to call the default constructor of the struct, resulting in initializing the variables declared in the struct.

Employee emp1;

Employee emp2;

emp1= new Employee (); emp1.EmployeeName = ‘Steve’ emp1.Employeeemail = ‘steve@hotmail.com’ emp1.EmployeeSalary = $1000;

Now, to copy these values from emp1 to emp2, you can use the assignment operator

(=) as follows:

emp2 = emp1;

As you can see, a single statement can copy the entire value of one struct to another. However, if you want to use classes to perform this task, you need to create a method. All data is copied from one struct to another; therefore, it is advisable that you create structs to store small amounts of data.

Structs in C# are different from structs in C and C++. Unlike C and C++, all data members of structs in C# are private by default.In addition, structs in C# are very similar to classes and can perform most of the things that classes do. However, some differences between structs and classes still exist.

As discussed earlier, structs do not require heap allocation. Instead, structs are stored on stacks of memory. This is how structs differ from classes. Figure 3-1 lists the key differences between structs and classes.

Another important component of C# is enumerations. Similar to classes and structs, enumerations are used to store values. The next section looks at enumerations in detail.

COMPONENTS OF C#

Chapter 3

 

61

 

FIGURE 3-1 Differences between structs and classes

Enumerations

Enumerations are data structures that store values with user-friendly names. This set of user-friendly named constants is called an enumerator list. The default data type of an enumerator list is an integer. Each enumerator has an integer basetype called the underlying-type. This underlying-type of the enumerator list must contain all the values that might be present in an instance of an enumerator. To use an enumerator in your code, you first need to declare the enumerator. Enumerators are declared using the enum keyword.

<modifier> enum <enumeration name>

{

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

}

The modifiers used with enumerations are new, public, private, protected, and internal. However, enumerations cannot be of the type abstract or sealed. Look at the following example to understand enumerations.

public enum months

{January, February, March, April, May, June, July, August, September, October, November, December};

62

Part II

HANDLING DATA

 

 

 

The previous code creates an enumeration with the name months and declares all the possible values for months. The first element of the enumeration takes a default value of 0 and the successive elements take the previous value plus 1.

You can also specify user-defined values for the elements of an enumeration. For example,

public enum months

{January = 1, February, March, April, May, June, July, August, September, October,

November, December};

In this case, the values of January, February, and March are 1, 2, and 3, respectively. In this chapter, you have learned about inheritance in classes. To implement inheritance, C# supports interfaces.

Interfaces

Interfaces are components used to declare a set of methods. However, the data members of an interface are not implemented. As discussed earlier, C# allows you to group related data by using structs. However, to group related methods, properties, indexers, and events, you use interfaces. Interfaces contain only method declarations; therefore, you cannot create an instance of an interface. However, you need to declare an interface by using the interface keyword.

interface <interface name>

{

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

}

Interface declarations do not include a modifier because all interfaces are public by default. Interfaces cannot be abstract, sealed, virtual, or static. However, you can use the new modifier with nested interfaces. The new modifier is used when you need to hide an inherited namespace by the same name as the base namespace.

In situations where you want the members of a class to exhibit certain features, you can group these members in an interface. The class can then implement the interface. The classes in C# can also implement multiple interfaces. Implementing an interface implies that a class is derived from the interface and the class

COMPONENTS OF C#

Chapter 3

63

 

 

 

implements all the members of that interface. Consider the following example of the Employee class that implements two interfaces:

interface Employee

{

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

}

interface Salary

{

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

}

class Employee: Employee, Salary

{

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

}

Therefore, the class Employee implements all the methods declared in the inter-

faces Employee and Salary.

Similar to classes, interfaces can also be inherited. These interfaces are called explicit base interfaces. C# does not support multiple inheritance of classes. However, you can achieve multiple inheritance in C# by using interfaces.

In the previous example, if the interface Salary was inherited from Employee, the interface declaration statement would be:

interface Salary: Employee

{

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

}

TIP

In C#, if a class implements an interface, the class also implicitly implements its base interfaces.

By now, you have learned about the basic components of C# that you can use to write programs in C#. The next sections look at writing a simple program in C# and then compiling and executing it.