Ad Code

Responsive Advertisement

Enumerator

Enumerated Property

using System;
namespace oopsProject
{

    public enum cities { Bangalore, Hyderabad, Mumbai, Kolkata, NewDelhi, Chennai };
    public class Customer
    {
        int _custid;
        string _cname, _state;
        double _balance;
        bool _status;
        cities _city;
        public Customer(int _custid)
        {
            this._custid = _custid;


            //Get all other values from DB basing on given ID:
            _cname = "prabhu";
            _balance = 5000;
            _status = false ;
            _city = 0;
            _state = "karnataka";
            Country = "India";
                       
        }
        //Read only Property
        public int Custid
        {
            get { return _custid; }
        }
        //Read write Property
        public string Cname
        {
            get { return _cname; }
            set { _cname = value; }
        }
        //Read write Property
        public bool Status
        {
            get { return _status; }
            set { _status= value; }
        }
        //Read write Property with a condition
        public double Balance
        {
            get
            {
                if
                    (_status)
                    return _balance;
                else return 0;
            }
            set
            {
                if (value >= 500)
                    _balance = value;
            }
            }
            // enumerated property
       
        public cities city
        {
        get{return _city;}
        set{_city = value;}
        }
        // setting scope of property Accessor independently (2.0)
        public string state
        {
            get { return _state;}
            protected set { _state = value;}
        }
        //Automatic Properties 3.0
            public string Country
            {
            get;
            private set;
            }
       
           }
    }

Enumerated Property

These are   property which is defined with a list of constants to choose from. to define these type of properties first we need to declare all the constants under a type known as “enum” .An enum is a collection of constants, which should be declared as following.

Syntax – 
[<modifiers>] enum   <name> {<list of values >}; 
EX:-

public enum Days {Monday, Tuesday, Wednesday, Thursday, Friday};


If this enum is used as a type for any property we call it as enumerated property.
- Declaring a variable of enum type.
Days weekday = 0;
or
Days weekday = Days.Wednesday ;

- Now for the variable weekday we can define a property as Following:
public Days weekday
{
get {return weekday;}
set { weekday = value;} 
}

New Features to Properties:-
<!--[if !supportLists]-->        <!--[endif]-->In C# 2.0, there are given an option to define a property setting the scope of each property Accessor independently.
Eg: State property we have defined under customer class.
<!--[if !supportLists]-->        <!--[endif]-->In C# 3.0, we were given with an option automatic properties, which allows us to define a property without any variable declaration for the property, So set and get blocks doesn’t contain any code in them, but it is must to define both get and set blocks.
Eg: Country Property in above customer class.

Consuming the Properties:-
- Add a class TestCustomer.cs and write the following:
using System;
namespace oopsProject
{
    class TestCustomer
    {
        static void Main()
        {
            Customer obj = new Customer(101);                                         
            Console.WriteLine(obj.Custid);
            //Assignment i not possible as property is read only
            //obj.Custid = 102;

            Console.WriteLine(obj.Cname);
            obj.Cname = "xyz";
            Console.WriteLine(obj.Cname);

            Console.WriteLine(obj.Status );
            //Balance will be zero here as status is inactive
            Console.WriteLine(obj.Balance);
            obj.Status = true;
            Console.WriteLine(obj.Status);
           
            // now it returns balance as status is active
           
            Console.WriteLine(obj.Balance);
            obj.Balance = 400; //assignment fails
           
            // here as it is less than 500

            obj.Balance = 400;
            Console.WriteLine(obj.Balance);

            // assignment succeeds as it is greater than 500

            obj.Balance = 600;
            Console.WriteLine(obj.Balance);
           
            Console.WriteLine(obj.city);
            obj.city = cities.Hyderabad;
            Console.WriteLine(obj.city);
            Console.WriteLine(obj.state);
            // Assigning value to the property is invalid as current
            //class is not a child class of customer
            //obj.state = "AP";
            Console.WriteLine(obj.Country);
            Console.ReadLine();

        }
    }
}
Note – Just like we have Abstract method and Virtual method, we can define Property also as abstract and virtual. So that we can override them under Child class.

Porperty

- Destructor are actually responsible for deallocation of memory used by objects but in managed languages deallocation is taken care by garbage collector for which destructor are not required, whereas in some situation we may be using any unmanaged resource like files, database etc. which will not be under control of garbage collector, in such scenario we use these destructors for managing those unmanaged resources.    

Properties

If at all a class contains any values in it that should be accessible outside of the class. Access to those values can be provided in two different ways –

  •     By storing the value in public variable access to the value can be given where anyone can get the value or set with a new value.

public  class Show
{
public int x = 100;
}
……………………….
Show obj = new Show ();
int a = obj x ;                               // Getting the value
int.x = 200;                                  // Setting the value
  • By storing the value in a private variable also access to the value can be given by defining a property on that variable, where we can provide the access to the value in three different ways.
        a.       Both get and set access (read | write property)
        b.      only get access (Read only property)
        c.       only set access (Write only property)
        Syntax:-
[<modifiers>]<type> <name>
{
[get {- stmts}]  // get Accessor
[set {- stmts}]  // Set Accessor
}
- The code which is present in the Get block is executed whenever we try to capture the value of property.
string str = “Hello”;
int len = str.Length;
- The code under set box gets executed whenever we are trying to assign in new value to the property.
Console.BackgroungColor = ConsoleColor.Blue;
-          If the property is defined with both get and set blocks it is a read | write property.
-          If defined only with get block, it is a read only property.
-          If defined only with a set block it is a write only property.
Reactions

Post a Comment

0 Comments