C# Access Modifiers

October 31, 2007 16:13 by WebStoryMaker

본 블로그 기사에서 사용된 예제 코드들은 MSDN2 (마이크로소프트) 에서 참조하였습니다.

C# 에서 사용되는 Access Modifier 에 대해 간략하게 정리하겠다. 다음의 네가지 Access Modifier 가 있다.

  • Public - 어떠한 제한이 없다.
// protected_public.cs
// Public access
using System;
class MyClass1 
{
   public int x; 
   public int y;
}
 
class MyClass2 
{
   public static void Main() 
   {
      MyClass1 mC = new MyClass1();
 
      // Direct access to public members:
      mC.x = 10;
      mC.y = 15;
      Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
   }
}
 
어떠한 제한도 없기 때문에 출력은 x = 10, y = 15 가 된다.
  • Protected - 클래스 안에서, 또는 파생된 클래스 안에서만 접근이 가능하다.

 

       1:  class A 
       2:  {
       3:     protected int x = 123;
       4:  }
       5:   
       6:  class B : A 
       7:  {
       8:     void F() 
       9:     {
      10:        A a = new A();  
      11:        B b = new B();  
      12:        a.x = 10;   // Error
      13:        b.x = 10;   // OK
      14:     }
      15:  }
     
    12번 줄에서 a.x 는 접근이 허용되지 않는다. 왜냐하면 A 로부터 파생된 클래스가 아니기 때문이다. 반면, b.x 는 허용이 된다.
       1:  // protected_keyword.cs
       2:  using System;
       3:  class MyClass 
       4:  {
       5:     protected int x; 
       6:     protected int y;
       7:  }
       8:   
       9:  class MyDerivedC: MyClass 
      10:  {
      11:     public static void Main() 
      12:     {
      13:        MyDerivedC mC = new MyDerivedC();
      14:   
      15:        // Direct access to protected members:
      16:        mC.x = 10;
      17:        mC.y = 15;
      18:        Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
      19:     }
      20:  }
     
    위의 예제의 경우에 출력 값은 x = 10, y = 15 가 된다.
  • Internal - 같은 어셈블리 안에서만 접근이 가능하다.
   1:  // Assembly1.cs
   2:  // compile with: /target:library
   3:  internal class BaseClass 
   4:  {
   5:     public static int IntM = 0;
   6:  }
 
   1:  // Assembly2.cs
   2:  // compile with: /reference:Assembly1.dll
   3:  // CS0122 expected
   4:  class TestAccess 
   5:  {
   6:     public static void Main() 
   7:     {
   8:        BaseClass myBase = new BaseClass();   
             // error, BaseClass not visible outside assembly
   9:     }
  10:  }
 
  • Private - 클래스의 Body 안에서, 또는 선언된 Struct 안에서만 접근 가능하다.
   1:  // private_keyword.cs
   2:  using System;
   3:  class Employee 
   4:  {
   5:     public string name = "xx";
   6:     double salary = 100.00;   // private access by default
   7:     public double AccessSalary() {
   8:        return salary;
   9:     }
  10:  }
  11:   
  12:  class MainClass 
  13:  {
  14:     public static void Main() 
  15:     {
  16:        Employee e = new Employee();
  17:   
  18:        // Accessing the public field:
  19:        string n = e.name; 
  20:        
  21:        // Accessing the private field:
  22:        double s = e.AccessSalary();    
  23:   
  24:        // Accessing the private field directly:
  25:        e.salary = 200.00 // error!
  26:     }
  27:  }

위의 경우 e.Salary 는 default 로 private 타입이 되기 때문에 접근이 불가능하다.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 20. 2008 14:42