C# 0 The Complete Reference


val  the value 10, which is the value of  num



Download 4,07 Mb.
Pdf ko'rish
bet742/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   738   739   740   741   742   743   744   745   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

val

 the value 10, which is the value of 



num

, which is pointed to by 



ip

. The 


operation of 

*

 can be remembered as “at address.” In this case, then, the statement could be 

read as “

val

 receives the value at address 



ip

.”

The



*

 can also be used on the left side of an assignment statement. In this usage, it sets 

the value pointed to by the pointer. For example,

*ip = 100;

This statement assigns 100 to the variable pointed to by 

ip

, which is 



num

 in this case. Thus, 

this statement can be read as “at address 

ip

, put the value 100.”



Using unsafe

Any code that uses pointers must be marked as unsafe by using the 



unsafe

 keyword. You 

can mark types (such as classes and structures), members (such as methods and operators), 

www.freepdf-books.com




588

 

P a r t   I :  



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

or individual blocks of code as unsafe. For example, here is a program that uses pointers 

inside

Main( )

, which is marked unsafe:

// Demonstrate pointers and unsafe.

using System;

class UnsafeCode {

  // Mark Main as unsafe.

  unsafe static void Main() {

    int count = 99;

    int* p; // create an int pointer

    p = &count; // put address of count into p

    Console.WriteLine("Initial value of count is " + *p);

    *p = 10; // assign 10 to count via p

    Console.WriteLine("New value of count is " + *p);

  }


}

The output of this program is shown here:

Initial value of count is 99

New value of count is 10



Using fixed

The


fixed

 modifier is often used when working with pointers. It prevents a managed 

variable from being moved by the garbage collector. This is needed when a pointer refers to 

a field in a class object, for example. Because the pointer has no knowledge of the actions of 

the garbage collector, if the object is moved, the pointer will point to the wrong object. Here 

is the general form of 



fixed

:

fi xed (



type* p

 = &


fi xedObj

) {


    // use fi xed object

}

Here, 



p

 is a pointer that is being assigned the address of an object. The object will remain at 

its current memory location until the block of code has executed. You can also use a single 

statement for the target of a 



fixed

 statement. The 



fixed

 keyword can be used only in an 

unsafe context. You can declare more than one fixed pointer at a time using a comma-

separated list.

Here is an example of 

fixed

:

// Demonstrate fixed.



using System;

class Test {

  public int num;

  public Test(int i) { num = i; }

}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   738   739   740   741   742   743   744   745   ...   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