C# 0 The Complete Reference


val  is passed to the  n



Download 4,07 Mb.
Pdf ko'rish
bet738/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   734   735   736   737   738   739   740   741   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

val

 is passed to the 



n

 parameter of 



AbsDivideBy( )

 and –2 is passed to the 



d

parameter.

As a point of interest, because the methods 

Reciprocal( )

 and 


AbsDivideBy( )

 are 


defined for 

double

, it is legal to invoke them on a 



double

 literal, as shown here:

8.0.Reciprocal()

8.0.AbsDivideBy(-1)

Furthermore, 

RevCase( )

 can be invoked like this:

"AbCDe".RevCase()

Here, the reversed-case version of a string literal is returned.

www.freepdf-books.com



20

Unsafe Code, Pointers

Nullable Types, and 

Miscellaneous Topics

T

his chapter covers a feature of C# whose name usually takes programmers by surprise: 



unsafe code. Unsafe code often involves the use of pointers. Together, unsafe code and 

pointers enable C# to be used to create applications that one might normally associate 

with C++: high-performance, systems code. Moreover, the inclusion of unsafe code and 

pointers gives C# capabilities that are lacking in Java.

Also covered in this chapter are nullable types, partial class and partial method 

definitions, and fixed-size buffers. The chapter concludes by discussing the few keywords 

that have not been covered by the preceding chapters.

Unsafe Code

C# allows you to write what is called “unsafe” code. Although this statement might seem 

shocking, it really isn’t. Unsafe code is not code that is poorly written; it is code that does 

not execute under the full management of the common language runtime (CLR). As 

explained in Chapter 1, C# is normally used to create managed code. It is possible, however, 

to write code that does not execute under the full control of the CLR. This unmanaged code 

is not subject to the same controls and constraints as managed code, so it is called “unsafe” 

because it is not possible to verify that it won’t perform some type of harmful action. Thus, 

the term 

unsafe

 does not mean that the code is inherently flawed. It simply means that it is 

possible for the code to perform actions that are not subject to the supervision of the 

managed context.

Given that unsafe code might cause problems, you might ask why anyone would want 

to create such code. The answer is that managed code prevents the use of 



pointers.

 If you are 

familiar with C or C++, then you know that pointers are variables that hold the addresses 

of other objects. Thus, pointers are a bit like references in C#. The main difference is that a 

pointer can point anywhere in memory; a reference always refers to an object of its type. 

Because a pointer can point anywhere in memory, it is possible to misuse a pointer. It is also 

585

CHAPTER


www.freepdf-books.com


586

 

P a r t   I :  



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

easy to introduce a coding error when using pointers. This is why C# does not support 

pointers when creating managed code. Pointers are, however, both useful and necessary for 

some types of programming (such as system-level utilities), and C# does allow you to create 

and use pointers. However, all pointer operations must be marked as unsafe since they 

execute outside the managed context.

The declaration and use of pointers in C# parallels that of C/C++—if you know how to 

use pointers in C/C++, then you can use them in C#. But remember, the essence of C# is the 

creation of managed code. Its ability to support unmanaged code allows it to be applied to a 

special class of problems. It is not for normal C# programming. In fact, to compile unmanaged 

code, you must use the 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   734   735   736   737   738   739   740   741   ...   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