Saturday 2 January 2016

What is property and Indexer in c#

  1. By Property we can evaluate the characteristics of any objects. 
  2. A property and function in c# is a wrapper meant for variables and elements, i.e it is an encapsulate entity.
  3.  A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field
  4. Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
  5. Bynproperty we can implement the validations.
  6. property used to implement the a business logic.
  7. A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. 
  8. The value keyword is used to define the value being assigned by the set accessor.
  9. Properties that do not implement a set accessor are read only.
  10. C# supports static properties.
  11. C# supports abstract properties

Auto implemented property:

1. auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

2. In C# 6 and later, you can initialize auto-implemented properties similarly to fields:
    eg. public string FirstName { get; set; } = "Jane";


Indexer:


An indexer allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a virtual array.

using System;/// <summary>
///
     A simple indexer example./// </summary>class IntIndexer
{
    private string[] myData;    public IntIndexer(int size)
    {
        myData = 
new string[size];        for (int i=0; i < size; i++)
        {
            myData[i] = "empty";
        }
    }

    public
 string this[int pos]
    {
        get
       {            return myData[pos];
        }
        set
       {
            myData[pos] = 
value;
        }
    }

    static
 void Main(string[] args)
    {
        int size = 10;

        IntIndexer myInd = 
new IntIndexer(size);

        myInd[9] = "Some Value";
        myInd[3] = "Another Value";
        myInd[5] = "Any Value";

        Console.WriteLine("\nIndexer Output\n");

        for
 (int i=0; i < size; i++)
        {
            Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
        }
    }
}


Overloaded Indexers:
using System;/// <summary>
///
     Implements overloaded indexers./// </summary>class OvrIndexer
{
    private string[] myData;    private int         arrSize;    public OvrIndexer(int size)
    {
        arrSize = size;
        myData = 
new string[size];        for (int i=0; i < size; i++)
        {
            myData[i] = "empty";
        }
    }

    public
 string this[int pos]
    {
        get
       {            return myData[pos];
        }
        set
       {
            myData[pos] = 
value;
        }
    }

    public
 string this[string data]
    {
        get
       {            int count = 0;
            for
 (int i=0; i < arrSize; i++)
            {
                if (myData[i] == data)
                {
                    count++;
                }
            }
            return count.ToString();
        }
        set
       {            for (int i=0; i < arrSize; i++)
            {
                if (myData[i] == data)
                {
                    myData[i] = 
value;
                }
            }
        }
    }

    static
 void Main(string[] args)
    {
        int size = 10;
        OvrIndexer myInd = 
new OvrIndexer(size);

        myInd[9] = "Some Value";
        myInd[3] = "Another Value";
        myInd[5] = "Any Value";

        myInd["empty"] = "no value";

        Console.WriteLine("\nIndexer Output\n");

        for
 (int i=0; i < size; i++)
        {
            Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
        }

        Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]);
    }
}






Diff. b/w Properties and Indexer:



Property
Indexer
Allows methods to be called as if they were public data members.
Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.
Accessed through a simple name.
Accessed through an index.
Can be a static or an instance member.
Must be an instance member.
get accessor of a property has no parameters.
get accessor of an indexer has the same formal parameter list as the indexer.
set accessor of a property contains the implicit valueparameter.
set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter.
Supports shortened syntax with Auto-Implemented Properties (C# Programming Guide).
Does not support shortened syntax.














No comments:

Post a Comment

Contact Us:

Email:

Vinodkumar434@gmail.com,
vinodtechnosoft@gmail.com

Skype Name:

vinodtechnosoft