Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi to all,

I have confusion on property and variable.

C#
public int ENo;

    public int EMPNO
    {
        get ; set;
    }


using above ENO variable we can read and write values, and using above EMPNO property we cam read and write values.

i don't want to set any condition to property.. if we put the condition to set property i know the difference, but here i didn't put any condition to property... then both plays same role.

What is the use of property here ? when we used these type of properties ?

Could anyone know reply to this.. ?


Regards
Nanda Kishore.CH
Posted
Updated 2-Sep-13 2:15am
v3

hii

Variable:

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.


for example
C#
int i, j, k;
char c, ch;
float f, salary;
double d;



Property:


Properties are named members of classes, structures, and interfaces. 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 accessors through which the values of the private fields can be read, written or manipulated.

Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.

For example, let us have a class named Student, with private fields for age, name and code. We cannot directly access these fields from outside the class scope, but we can have properties for accessing these private fields.

C#
Example:
The following example demonstrates use of properties:

using System;
class Student
{

   private string code = "N.A";
   private string name = "not known";
   private int age = 0;

   // Declare a Code property of type string:
   public string Code
   {
      get
      {
         return code;
      }
      set
      {
         code = value;
      }
   }
   
   // Declare a Name property of type string:
   public string Name
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }

   // Declare a Age property of type int:
   public int Age
   {
      get
      {
         return age;
      }
      set
      {
         age = value;
      }
   }
   public override string ToString()
   {
      return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
   }

   public static void Main()
   {
      // Create a new Student object:
      Student s = new Student();
            
      // Setting code, name and the age of the student
      s.Code = "001";
      s.Name = "Zara";
      s.Age = 9;
      Console.WriteLine("Student Info: {0}", s);
      //let us increase age
      s.Age += 1;
      Console.WriteLine("Student Info: {0}", s);
      Console.ReadKey();
    }
}
When the above code is compiled and executed, it produces following result:

Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10
 
Share this answer
 
Comments
ridoy 2-Sep-13 5:02am    
good explanation,my 5!
Priyanka Bhagwat 2-Sep-13 5:05am    
Thanks Ridoy..:)
Dholakiya Ankit 2-Sep-13 8:15am    
good one my 5ed
Herbisaurus 2-Sep-13 10:29am    
5+!
This comes from object oriented concept. Data hiding or encapsulate the data into an object.
for example, you have 10 value field to use inside the class but you want to use 5 property which will be used outside the class by instantiating your object.
 
Share this answer
 
Hello,
In property we can put some validations or can do any operation on the field before set or get of value, but in case of member variable it is not there.

Please check bellow link for better understanding.

http://stackoverflow.com/questions/4142867/what-is-difference-between-property-and-variable-in-c-sharp[^]
 
Share this answer
 
According to Object oriented Concept:
ENo variable is called field and it is not encapsulate.
EMPNO variable is called property and it is encapsulate.
So, both are variable but EMPNO have an extra encapsulation attribute.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900