blog

Home / DeveloperSection / Blogs / Properties

Properties

Anchal Kesharwani 2398 12-Aug-2014

In this blog, I’m explaining the concept of properties.

Properties are special kind of class members.  Member variables or methods in a class or structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use assessors through which the values of the private fields can be read, written or manipulated.

We use predefined set and get methods to access and modify them. Property reads and writes are translated to get and set method calls.

Get

The get { } implementation must include a return statement. It can access any member on the class.

Set

The set { } implementation receives the implicit argument "value." This is the value to which the property is assigned.

Properties have many uses: they can validate data before making free to  a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.

Features of Properties

·     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.

·   A property may be declared as a static property by using the static keyword.

Example

There is an example student personal details.

using System;
namespace PropertiesExample
{
    publicclassStudent
    {
        publicstaticint NumberOfStudents;
        privatestaticint count=1;
        privatestring name;
        // A read-write instance property of Student class
        publicstring Name
        {
            // here, get method is used to get the name
            get {
                return name;
            }
            // here, set method is used to get the name
            set {
                name = value;
            }
        }
        // A read-only static property of Student class
        publicstaticint Counter
        {
            // here, only get count not set
            get { return count; }
        }
        // A Constructor:
        public Student()
        {
            // Calculate the Student's number:
            count = count + NumberOfStudents;
        }
    }
    classTest
    {
        staticvoid Main()
        {
            Student.NumberOfStudents = 1000;    // number of student: 1000
            Student studentObj = newStudent();
            studentObj.Name = "Manoj pandey";   // a new student 'manoj pandey'
System.Console.WriteLine("Student number: "+Student.Counter);   // student number is total no. of student +1
       System.Console.WriteLine("Student name: "+studentObj.Name);   // student name
       Console.ReadKey(); // hold the screen     
 }
    }
}

Output:

Student number: 1001

Student name: Manoj pandey

In this example, a new student added after number of student+1 by using get and set method.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By