Labels

Monday, March 9, 2009

Properties Myth

Properties Myth

 

-          Properties can be marked as public, private, protected, internal, or protected internal. These access modifiers define how users of the class can access the property.

-          The get and set accessors for the same property may have different access modifiers. But both get and set could not have the modifiers simultaneously.

-           

-          A property may be declared as a Static property by using the static keyword. Provided the age variable is also ‘Static’

-          A property may be marked as a Virtual property by using the virtual keyword.

-          A property overriding a virtual property can also be Sealed, specifying that for derived classes it is no longer virtual.

 

Parameterized Properties

 

C# does not support parametrized properties as VB.Net. However this can be achieved using Indexers.

 

class Employee

{

            public string city;

            public string state;

 

            public string this[string strCity]  // Indexer

            {

                get

                {

                  return state;

                }

               set

               {

                  state = value;

               }

            }

}

Valid.

 

The call to below shoul look like below.

 

Employee emp = new Employee();

emp["ABU"] = "Rajasthan";

class ColorManager

    {

        Hashtable colors = new Hashtable();

 

        // Indexer

        public Color this[string name]

        {

            get

            {

                return colors[name] as Color;

            }

            set

            {

                colors.Add(name, value);

            }

        }

    }

class Color

    {

        private int red;

        private int green;

        private int blue;

 

        // Constructor

        public Color(int red, int green, int blue)

        {

            this.red = red;

            this.green = green;

            this.blue = blue;

        }       

    }

 

static void Main()

{

    ColorManager colormanager = new ColorManager();

 

    colormanager["red"] = new Color(255, 0, 0);

    colormanager["green"] = new Color(0, 255, 0);

    colormanager["blue"] = new Color(0, 0, 255);

}

 

 

VB.Net supports parameterized properties as below.

 

Private colPhones As New Hashtable()

 

Public Property Phone(ByVal Location As String) As String

    Get

      Return CStr(colPhones.Item(Location))

    End Get

 

    Set(ByVal Value As String)

      If colPhones.ContainsKey(Location) Then

        colPhones.Item(Location) = Value

      Else

        colPhones.Add(Location, Value)

      End If

    End Set

  End Property

 

 

 

ClassName.Phone(“Home”)=”1234”  

String myphone= ClassName.Phone(“Home”)

 

 

 

Default Properties

 

-          Must be parameterized property. i.e You can create default properties that accept parameters (such as the Item property of a collection.

-          This leads to - Default Properties can only be implemented using Indexers.

 

 

public class Widgets

{

   // Indexer implementation.

   public Widget this[int index]

   {

      get

      {

         // Insert code to return a Widget.

      }

      set

      {

         // Insert code to set a Widget.

      }

   }

}

 

 

 

 

 

 

Other Myths about Properties -

 

public int ReadOnlyAge

{

    get

    {return age; }

}

 

Valid

public int WriteOnlyAge

{

    set

    {

            age = value;

    }

}

 

Valid

public int PrivateAge

{          

    private set

    {

            age = value;

    }

}

 

InValid -  Acessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor.

public int PrivateAge

{

    get

    {

            return age;

    }

    private set

    {

            age = value;

    }

}

 

Valid

public int PrivateAge

{

    get

    {

            return age;

    }

    Public set

    {

            age = value;

    }

}

 

Invalid -  'PrivateAge.set' accessor must be more restrictive than the property or indexer 'PrivateAge'

public int PrivateAge

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

Valid

Protected int PrivateAge

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

 

Invalid -  'PrivateAge.set' accessor must be more restrictive than the property or indexer 'PrivateAge'

Protected int PrivateAge

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

 

Valid

public int PrivateAge

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid

public int PrivateAge

{

    Public get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Invalid -  'PrivateAge.get' accessor must be more restrictive than the property or indexer 'PrivateAge'

public int PrivateAge

{

    Protected get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid

public int PrivateAge

{

    Public get

    {

            return age;

    }

    Public set

    {

            age = value;

    }

}

 

Invalid -  'PrivateAge.get' accessor must be more restrictive than the property or indexer 'PrivateAge'

 

Invalid -  'PrivateAge.set' accessor must be more restrictive than the property or indexer 'PrivateAge'

public int PrivateAge

{

    Private get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

Invalid -  Cannot specify accessibility modifiers for both accessors of the property.

public int PrivateAge

{

    Protected get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

Invalid -  Cannot specify accessibility modifiers for both accessors of the property.

public int PrivateAge

{

    Private get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

Invalid -  Cannot specify accessibility modifiers for both accessors of the property.

public virtual int PrivateAge

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid.

public virtual int PrivateAge

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

Valid. Also valid for Protected.

 

public virtual int PrivateAge

{

    Private get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid. Also valid for Protected.

 

 

public static int PrivateAge

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid – Provided the age variable is also ‘Static’

public static int PrivateAge

{

    Private get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid – Provided the age variable is also ‘Static’.

Also valid for Protected.

public static int PrivateAge

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

Valid – Provided the age variable is also ‘Static’.

Also valid for Protected.

public virtual int PrivateAge

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid

public virtual int PrivateAge // Parent Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

 

Valid.

Note- This overriding is in the derived class.

public virtual int PrivateAge // Parent Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

public override sealed int PrivateAge // Derived

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

Valid

Note- This overriding is in the derived class.

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

 

Invalid – Derived Class cannot override because Parent Class does not have an overridable set accessor.

 

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

}

 

 

Valid.

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

 

Invalid - Derived Class cannot cannot change access modifiers when overriding 'protected' inherited member.

 

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    Private set

    {

            age = value;

    }

}

 

 

Invalid - Derived Class cannot cannot change access modifiers when overriding 'protected' inherited member.

 

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

 

Invalid - Derived Class cannot cannot change access modifiers when overriding 'public' inherited member.

 

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

 

Valid.

 

Note – Same applied for ‘get’

public virtual int PrivateAge  // Parent Class

{

    get

    {

            return age;

    }

    set

    {

            age = value;

    }

}

 

public override int PrivateAge // Derived Class

{

    get

    {

            return age;

    }

    Protected set

    {

            age = value;

    }

}

 

 

Invalid - Derived Class cannot cannot change access modifiers when overriding 'public' inherited member.

 

Note – Same applied for ‘get’

 

 

Regards,

Arun Manglick

 

 

 

No comments:

Post a Comment