Tuesday, 2024-12-24, 11:06 PM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
How is .Net classes by R.N.Reddy sir ?
Total of answers: 243
Statistics

Total online: 1
Guests: 1
Users: 0


20-12-2010


Property: It is a member of a class, which is defining the shape of the object.

Property definition will have 2 accessor
    1. get accessor
    2. set accessor


Syntax for property definition:
    <access modifier> <return type> <property name>
    {
        get
        {
        }
       set
        {
        }
    }


Set accessor will execute when we are assigning a value to property.

Get accessor will execute when we are retrieving the property content or when we are displaying the property value.

Eg: For int property:
    namespace propertyexample
    {
      Public class myclass
        {
            Public int x; // x is a variabe
            Public int X // X is a property
             {
                get
                 {
                     return x;
                  }
                set
                 {
                    x= value;
                  }
              }

    Clas program
    {
       void main()
        {
           myclass mc = new myclass();
           mc.X = 25; // set accessor will call
           Console.WriteLine("X property value is " +mc.X);
           Console.ReadLine();
        }
     }
  }
Output: X property value is 25

While defining a property we have to use one predefined variable value and user defined variable.

In the above program 'x' is a user defined variable. In general in property defination what ever user defined variable we will use will declare that variable name as same as property name but case differ it is for just programmer convience.

For eg: In this above program, X is a property and 'x' is a user defined variable.

Eg: For a string property:
    namespace propertyexample
    {
      Public class myclass
       {
          Public string city; // city is a variabe
          Public string CITY // CITY is a property
            {
              get
               {
                  return city;
               }
              set
              {
                 city= value;
               }
            }
    
       Class program
        {
          void main()
           {
               myclass mc = new myclass();
               mc.CITY = "Hyderabad"; // set accessor will call
               Console.WriteLine("City name is " +mc.X); // get will call
               Console.ReadLine();
           }
        }
    }
Output: City name is Hyderbad

Access modifiers (or) Access specifiers:

Access modifiers are keywords which are specifying the accessibility or access level of a class or class members.

In C#.NET access modifiers are classified into 5 types:
             1. Public
             2. Private
             3. Protected
             4. Internal
             5. Protected Internal


1. Public: If we declare a class or class member access modifier as a public which can be accessed by all the classer & all the project members within that application.

Eg: For Public access modifier:

namespace publicexample
  {
    Public class class1
     {
       Public int a =10;
       Public void method1()
          {
            Console.WriteLine("Method1 value is " +a);
          }
    }

   Public class class2
    {
      Class1 c1obj = new class1();
      Public void method2()
         {
            Console.WriteLine("Method2 value is " +c1obj.a);
         }
   }

Class program
  {
     Void main()
        {
           Class1 c1obj2 = new class1();
           Console.WriteLine("a value is " +c1obj2.a);
           c1obj2.method1();

           Class2 c2obj1 = new class2();
           c2obj1.method2();
           Console.ReadLine();
        }
   }
}
Output: a value is 10
              Method1 value is 10
              Method2 value is 10


Search

Time
 

 

Calendar
 
«  December 2024  »
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
293031

Twitter
 

Entries archive

Copyright R N Reddy © 2024Developed by Nagendra, Venkat, Vijaya and Yaswanth
Get free updates as SMS to your mobile. Just register by clicking here