Thread: private/public get/set

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    private/public get/set

    Hello everyone,


    New to C#, a simple question which my book does not cover.

    If we do not specify the public/private access of get/set, then it is of the same as the public/private access to the property itself, but we can overwrite it.

    For example, in the following code, in get method, when we do not specify public/private, it will be automatically the same as the property Abc, which makes get public, but in set, we can overwrite it to make it private?

    Code:
    public class MyList
    {
    
        class Foo
        {
            private int _abc;
    
            public int Abc
            {
                get
                {
                    return _abc;
                }
                private set
                {
                    _abc = value;
                }
            }
    
        }
    
        static void Main()
        {
            Foo f = new Foo();
            return;
        }
    }

    thanks in advance,
    George

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    we can overwrite it to make it private?
    yes we can
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    Quote Originally Posted by vart View Post
    yes we can
    I have written some samples and here are the learning results. Could you review whether I am correct. :-)

    http://msdn.microsoft.com/en-us/library/75e8y5dd.aspx

    1.

    "You cannot use accessor modifiers on an interface or an explicit interface member implementation."

    Means we can not put modifier on interface declaration of accessor, and also can not use modifier on the class implementation of the accessor (which is declared in interface).

    2.

    "When you use an accessor to implement an interface, the accessor may not have an access modifier. However, if you implement the interface using one accessor, such as get, the other accessor can have an access modifier, as in the following example:"

    It means if interface only declare one accessor, either get/set, then in the class implementation, for the interface declared accessor, either get/set, we can not use any modifier, but for the other accessor, which is not declared in interface, either set/get, we can add modifier.

    Both understandings are correct?


    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private/Public Classes, explanation please in context
    By porsche911nfs in forum C++ Programming
    Replies: 9
    Last Post: 04-22-2009, 08:37 PM
  2. get/set
    By C_ntua in forum C++ Programming
    Replies: 21
    Last Post: 09-26-2008, 02:23 PM
  3. Trivial: Get/Set + Function Order in Classes
    By Ashes999 in forum C++ Programming
    Replies: 10
    Last Post: 07-08-2003, 11:09 AM