Open In App

C# | Restrictions on Properties

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Properties in C#

Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and help to promote the flexibility and safety of methods. Encapsulation and hiding of information can also be achieved using properties. It uses pre-define methods which are “get” and “set” methods which help to access and modify the properties.

Syntax:

<access_modifier> <return_type> <property_name>
{
        get { // body }
        set { // body }
}

Restrictions on Properties: We have to follow a few rules or restrictions while writing properties which are as follows:

  • A property cannot be passed via ref or out parameter to a method: Properties cannot be passed via out or ref, as properties, are actually methods. Both ref and out are not considered to be a part of method signature at the time of compilation. Due to this, it will give a compile-time error. There is no solution to this restriction except to remove ref or out from the program entirely.
  • You cannot overload a property: A property cannot be overloaded. It means that one can only put a single get and set accessor and mutator in a class respectively. The program given below shows what happens when we give more than one get accessor in the same class.




    // C# program to show what happens when
    // we try to overload a property
    using System;
      
    class Myprop {
      
        int _number;
      
        public int Number
        {
            get
            {
                return this._number;
            }
            set
            {
                this._number = value;
            }
            get
            {
      
                // Causes a compile time error
                // which tells get accessor
                return this._number; 
      
            } // has already been called.
        }
    }
      
    // Driver Class
    class GFG {
      
        // Main Method
        static void Main()
        {
            Myprop ex = new Myprop();
      
             // set { }
            ex.Number = 5;
            console.WriteLine("If there are only two properties"+
                          " in class ex we will get the output");
      
            // get { }
            Console.WriteLine(ex.Number); 
        }
    }

    
    

    Compile Time Error:

    prog.cs(19,3): error CS1007: Property accessor already defined

    But by using properties in different classes in the same program, one can use the get and set multiple times. An example illustrating this is given below.




    // C# program to demonstrate the use
    // of properties in different classes
    using System;
      
    class Myprop {
      
        int _number;
      
        public int Number
        {
            get
            {
                return this._number;
            }
            set
            {
                this._number = value;
            }
        }
    }
      
      
    class Myprops {
      
        int _number;
        public int Number
        {
            get
            {
                return this._number;
            }
            set
            {
                this._number = value;
            }
        }
    }
      
    // Driver Class
    class GFG {
      
        // Main Method
        static void Main()
        {
            Myprop ex = new Myprop();
            Myprops exs = new Myprops();
      
            // Calls set mutator from the Myprops class
            exs.Number = 7; 
      
            // Calls set mutator from the Myprop class
            ex.Number = 5; 
      
            // Gets the get accessor form the Myprop class
            Console.WriteLine("The value set in the class "+
                                 "Myprop is: " + ex.Number); 
      
            // Gets the get accessor form the Myprops class
            Console.WriteLine("The value set in the class"+
                             " Myprops is: " + exs.Number); 
        }
    }

    
    

    Output:

    The value set in the class Myprop is: 5
    The value set in the class Myprops is: 7
    
  • A property should not alter the state of the underlying variable when the get accessor is called: The get accessor of properties is preset to read-only mode while the set command is set to write-only mode. Due to this, if we try to enter values or modify in the scope of the get accessor we’ll get a warning which tells that the code we are trying to access is unreachable. The example given below illustrates that.




    // C# program to demonstrate the 
    // above-mentioned restriction
    using System;
      
    class Myprop {
      
        int _number;
        public int Number
        {
            get
            {
                return this._number;
      
                // We get the warning saying that
                // the code is unreachable.
                this._number = 5; 
            }
            set
            {
                this._number = value;
            }
        }
    }
      
    // Driver Class
    class GFG {
      
        static void Main()
        {
            Myprop ex = new Myprop();
      
            // set { }
            ex.Number = 5; 
      
             // get { }
            Console.WriteLine(ex.Number);
        }
    }

    
    

    Warnings:

    prog.cs(16,4): warning CS0162: Unreachable code detected

    Output:

    5

    As seen by the output of the program, the code is run successfully but the values that we add in the get accessor cannot be used as it is unreachable. That is why there is no use of changing the underlying variable when the get accessor is called.



Last Updated : 22 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads