C# 0 The Complete Reference


int , use an  int



Download 4,07 Mb.
Pdf ko'rish
bet407/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   403   404   405   406   407   408   409   410   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

int

, use an 



int

 variable; 

when you need to store a 

string

, use a 


string

 reference; and so on.

More importantly, since version 2.0, true generic types are available to the C# programmer. 

(Generics are described in Chapter 18.) The addition of generics enables you to easily define 

classes and algorithms that automatically work with different types of data in a type-safe 

manner. Because of generics, you will normally not need to use 



object

 as a universal type 

when creating new code. Today, it’s best to reserve 

object

’s universal nature for specialized 

situations.

www.freepdf-books.com




12

Interfaces, Structures, and 

Enumerations

T

his chapter discusses one of C#’s most important features: the interface. An 



interface

defines a set of methods that will be implemented by a class. An interface does not, 

itself, implement any method. Thus, an interface is a purely logical construct that 

describes functionality without specifying implementation.

Also discussed in this chapter are two more C# data types: structures and enumerations. 

Structures

 are similar to classes except that they are handled as value types rather than 

reference types. 

Enumerations

 are lists of named integer constants. Structures and enumerations 

contribute to the richness of the C# programming environment.

Interfaces

In object-oriented programming it is sometimes helpful to define what a class must do, but 

not how it will do it. You have already seen an example of this: the abstract method. An 

abstract method declares the return type and signature for a method, but provides no 

implementation. A derived class must provide its own implementation of each abstract 

method defined by its base class. Thus, an abstract method specifies the 



interface

 to the 


method, but not the 

implementation.

 Although abstract classes and methods are useful, it is 

possible to take this concept a step further. In C#, you can fully separate a class’ interface 

from its implementation by using the keyword 



interface

.

Interfaces are syntactically similar to abstract classes. However, in an interface, no 



method can include a body. That is, an interface provides no implementation whatsoever. 

It specifies what must be done, but not how. Once an interface is defined, any number of 

classes can implement it. Also, one class can implement any number of interfaces.

To implement an interface, a class must provide bodies (implementations) for the 

methods described by the interface. Each class is free to determine the details of its own 

implementation. Thus, two classes might implement the same interface in different ways

but each class still supports the same set of methods. Therefore, code that has knowledge of 

the interface can use objects of either class since the interface to those objects is the same. By 

providing the interface, C# allows you to fully utilize the “one interface, multiple methods” 

aspect of polymorphism.

311

CHAPTER


www.freepdf-books.com


312

 

P a r t   I :  



T h e   C #   L a n g u a g e

Interfaces are declared by using the 



interface

 keyword. Here is a simplified form of an 

interface declaration:

interface



name

 {

ret-type method-name1

(

param-list

);

ret-type method-name2

(

param-list

);

   // ...



ret-type method-nameN

(

param-list

);

}

The name of the interface is specified by 



name.

 Methods are declared using only their return 

type and signature. They are, essentially, abstract methods. As explained, in an interface, 

no method can have an implementation. Thus, each class that includes an interface must 

implement all of the methods. In an interface, methods are implicitly 

public

, and no explicit 

access specifier is allowed.

Here is an example of an interface. It specifies the interface to a class that generates a 

series of numbers.

public interface ISeries {

  int GetNext(); // return next number in series

  void Reset(); // restart

  void SetStart(int x); // set starting value

}

The name of this interface is 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   403   404   405   406   407   408   409   410   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish