C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet233/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   229   230   231   232   233   234   235   236   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

C#’s Access Modifiers

Member access control is achieved through the use of four 



access modifiers

:

public

,

private

,

protected

, and 

internal

. In this chapter, we will be concerned with 



public

 and 


private

. The 


protected

 modifier applies only when inheritance is involved and is described in Chapter 11. 

165

CHAPTER


www.freepdf-books.com


166

 

P a r t   I :  



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

The


internal

 modifier applies mostly to the use of an 



assembly, 

which for C# loosely means 

a deployable program or library. The 

internal

 modifier is examined in Chapter 16.

When a member of a class is modified by the 

public

 specifier, that member can be 

accessed by any other code in your program. This includes methods defined inside other 

classes.


When a member of a class is specified as 

private

, then that member can be accessed only 

by other members of its class. Thus, methods in other classes are not able to access a 

private

member of another class. As explained in Chapter 6, if no access specifier is used, a class 

member is private to its class by default. Thus, the 

private

 specifier is optional when creating 

private class members.

An access specifier precedes the rest of a member’s type specification. That is, it must 

begin a member’s declaration statement. Here are some examples:

public string errMsg;

private double bal;

private bool isError(byte status) { // ...

To understand the difference between 

public

 and 


private

, consider the following 

program:

// Public vs. private access.

using System;

class MyClass {

  private int alpha; // private access explicitly specified

  int beta;          // private access by default

  public int gamma;  // public access

  // Methods to access alpha and beta. It is OK for a member

  // of a class to access a private member of the same class.

  public void SetAlpha(int a) {

    alpha = a;

  }


  public int GetAlpha() {

    return alpha;

  }

  public void SetBeta(int a) {



    beta = a;

  }


  public int GetBeta() {

    return beta;

  }

}

class AccessDemo {



  static void Main() {

    MyClass ob = new MyClass();

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   229   230   231   232   233   234   235   236   ...   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