Accessor

Dictionary Meaning:

The meaning of “Accessor” is “Someone or something that accesses”.

What is Accessor?

In C# programming, the accessor function is like getter accessor (get) and setter accessor (Set) functions. This is used to change the class member access label by set to public.

In OOP aspects as it provides an abstraction layer that hides the implementation details of functionality sets.

For example:

namespace accessor
{
    class myName
    {
        private string FullName;
        public string _FullName
        {
            get
            {
                return FullName;
            }
            set
            {
                FullName = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            myName objDt = new myName();
            objDt._FullName = "Shashangka Shekhar";
            Console.WriteLine(objDt._FullName);
            Console.ReadLine();
        }
    }
}

The get accessor:

Get accessor return/retrive value from private field.

For example:

class myName
    {
        private string FullName;
        public string _FullName
        {
            get
            {
                return FullName;
            }
        }
    }

The set accessor:

Set accessor is to assign/storing a value in private variables. It uses an implicit parameter called value.

For example:

class myName
    {
        private string FullName;
        public string _FullName
        {
            set
            {
                FullName = value;
            }
        }
    }

When we assign a value to the property, the set accessor is invoked by using an argument that provides the new value.

For example:

myName objDt = new myName();
objDt._FullName = "Shashangka Shekhar"; // the set accessor is invoked here
Console.WriteLine(objDt._FullName); // the get accessor is invoked here
Console.ReadLine();

Note:

  1. A property without a set accessor is considered read-only.
  2. A property without a get accessor is considered write-only.
  3. A property that has both accessors is read-write.

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

Leave a Reply