Tuesday, December 4, 2012

A simple 'property' program

What is properties:
  • Properties are like data fields, but have logic behind them
  • From the outside, they look like any other member variable
              But they act like a member function
  • Defined like a field, with 'get' and 'set' code added
  • Can use access modifiers like fields and methode can

Why use Properties?

  • properties can be calculated on-the-fly
  • provide complete control over data field access
            logic is hidden from the object's consumer
  • Properties promote the idea of encapsulation

 //A simple program to implement property
//using c#

using System;

class company
{
    public void interview_sch()
    {
        Console.WriteLine ( "your interview is schedule" );
    }
}

class candidate
{
    private string skills;
   
    public string can_skills                // This
    {                                             // is
        get                                      // 
        {                                         // P
            return skills;     //retrieve    // R
        }                                         // O
                                                   // P
        set                                      // E
        {                                        // R
            skills = value;   //assign      // T
        }                                        // Y
    }                                            //
}

class consultant
{
    public static void Main()
    {
        candidate anil = new candidate();
        anil.can_skills = "java";
        string s = anil.can_skills;
       
        if (s == "c")
        {
            company c = new company();
            c.interview_sch();
        }
       
        else
        {
            Console.WriteLine ( "your criteria deoesn't match" );
        }
    }
}

No comments:

Post a Comment