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

问题描述

大家好,



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



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

推荐答案

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




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

07-23 17:59
查看更多