Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Properties

Property is a class member that looks like fields or variables if you try to access using objects, but they use accessor methods to get and set their data.

Points to remember:

1.  This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
2.  Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
3.  A get property accessor is used to return the property value, and a set accessor is used to assign a new value.
4.  Properties that do not implement a set accessor are read only.
5.  For auto implemented properties no custom accessor required.

get accessor:

get acessor is like method,It must return a value of the property type.
When you reference the property, except as the target of an assignment, the get accessor is invoked to read the value of the property

set accessor:

 Set accessor is like method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property.
When you assign a value to the property, the set accessor is invoked by using an argument that provides the new value.

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Syntax:

[attributes] [new] type identifier {interface-accessors}

 

more info...http://codechef4u.com/post/2015/07/02/Interface-Properties


Read-only Properties:

You can also create read-only properties if you avoid setting accessor method. For example, to

make the Name property a read-only property, you use this code:

    class Product
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }
    }


Static Properties:

You can create static properties in C# like fields, which means you don’t need an object to use them.

    public class student
    {
        private static int numberOfStudents = 0;
        public student()
        {
            numberOfStudents++;
        }
 
        public static int NumberOfStudents
        {
 
            get
            {
                return numberOfStudents;
            }
            set
            {
                numberOfStudents =value;
            }
        }
    }

 

Auto implemented properties:

In C#3.0 and above Microsoft introduced feature called auto implemented properties. 

For auto implemented properties no custom accessor required. They also enable client code to create objects. When you declare a property, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Example:

public class Product
    {
        string ProductUseCategory;
 
        private string name;
 
 
        public Product(string cetgory)
        {
            ProductUseCategory =cetgory;
        }
       
        //auto implemented properties
        public int ProductID { get; set; }
 
        //simple property declartion
        public string ProductName
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
     
        public double? UnitPrice { get; set; }
        
        public bool isProductSellable()
        {
            if (ProductUseCategory== "Expired")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
       
    }
 

Invalid entry,please enter valid data.

Loading