变量和属性之间的区别? [英] Difference Between variable and property ?

查看:131
本文介绍了变量和属性之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对财产和变量感到困惑。



Hi to all,

I have confusion on property and variable.

public int ENo;

    public int EMPNO
    {
        get ; set;
    }





使用上面的ENO变量我们可以读取和写入值,并使用上面的EMPNO属性我们凸轮读写值。



i不想设置属性的任何条件..如果我们把条件设置属性我知道差异,但在这里我没有放任何条件财产...然后两个扮演相同的角色。



这里的财产有什么用?当我们使用这些类型的房产?



有谁知道回复这个...?





问候

Nanda Kishore.CH



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

推荐答案

hii



变量:



变量只不过是我们的程序可以操作的存储区域的名称。 C#中的每个变量都有一个特定的类型,它决定了变量内存的大小和布局;可以存储在该存储器中的值的范围;以及可应用于变量的操作集。





例如

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
int i, j, k;
char c, ch;
float f, salary;
double d;







属性:




属性是类,结构和接口的命名成员。类或结构中的成员变量或方法称为Fields。属性是字段的扩展,可以使用相同的语法进行访问。它们使用访问器,通过这些访问器可以读取,写入或操作私有字段的值。



属性不命名存储位置。相反,他们有读取,写入或计算其值的访问器。



例如,让我们有一个名为Student的类,其中包含年龄,名称和码。我们无法直接从类范围外访问这些字段,但我们可以使用属性来访问这些私有字段。






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.

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


这来自面向对象的概念。数据隐藏或将数据封装到对象中。例如,
,你有10个值字段要在类中使用,但你想使用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.


你好,

在属性中我们可以设置一些验证,或者可以在设置或获取值之前对字段进行任何操作,但是在成员变量的情况下不存在。



请查看下面的链接以便更好地理解。



http://stackoverflow.com/questions/4142867/what-is-difference-之间的属性和变量在尖锐的 [ ^ ]


这篇关于变量和属性之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆