
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
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®

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.

64 |
Part II |
HANDLING DATA |
|
|
|
Writing, Compiling, and Executing
a C# Program
Writing a C# Program
Writing a program in C# involves writing the Main() method. Before w riting a program, you need to select the template from the available templates for C#. C# provides you with a variety of templates, as shown in Figure 3-2.
FIGURE 3-2 Templates for writing a C# program
The execution of a C# program starts with the execution of the Main() method. Therefore, you need to write the Main() method for each program in C#. The Main() method is of the type static and returns a value of the type void or int.
If the Main() method is of the type void, it does not return a value. However, a Main() method of the type integer returns an integer type variable. The following is the syntax of a Main() method.
<modifier> static <data type> Main ()
Here, modifier is the access modifier of the Main() method and the data type is
void or integer.

COMPONENTS OF C# |
Chapter 3 |
65 |
|
|
|
The modifier of the Main() method is explicitly written as public. However, it would not make a difference if any other modifier is specified.
The next code is an example of writing a simple program in C#.
using System; class Class1
{
public static void Main()
{
Console.WriteLine (“This is a sample program in C#”);
}
}
The code uses the using statement to enable you to use the System namespace in the program code.The class keyword is then used to declare a class by the name Class1. Inside the class declaration is the static method Main() of the type void. The Console.WriteLine statement is used to display the text given in double quotes (“ ”) in the Console window.
Compiling a C# Program
Once you have written a program, you can compile the program by using the Build command in the Build menu. The compilation of the program is shown in Figure 3-3.
FIGURE 3-3 Compiling a C# prog ram

66 |
Part II |
HANDLING DATA |
|
|
|
Executing a C# Program
After compiling the C# program, you need to execute the program. For executing a program, click the Start command in the Debug menu.
An example of executing a C# program is shown in Figure 3-4.
FIGURE 3-4 Executing a C# program
Summary
In this chapter, you learned about the components of C#. These components include classes, namespaces, structs, enumerations, and interfaces. A class is defined as a data structure used to create objects. The instance of a class is called an object. Next, you learned about the methods used in classes. A method is a logical section of code that can be used to perform a specific operation. A method has a return type or can be of the type void.
Another important component of C# is a namespace, which is a container used to logically group similar classes. C# also includes structs, which are data structures containing constructors, constants, variables, methods, indexers, properties, operators, and nested types. You also leaned about enumerations, which are data structures that store values with user-friendly names. You also learned about interfaces, which are components used to declare a set of methods. However, the data members of the interface are not implemented. Finally, you learned to write, execute, and compile a simple program in C#.

Chapter 4
More about Components