1.49. C# property

发布时间 : 2025-10-25 13:34:38 UTC      

Page Views: 15 views

Properties are named members of classes, structures and interfaces. A member variable or method in a class or structure is called a Field. The Property is an extension of the Field and can be accessed using the same syntax. They use accessors to make private domain values read, written or manipulated.

Property does not determine the storage location. Instead, they have an accessor that reads, writes or calculates their values.

For example, there is a person named the private domain of Student with the age name and code . We cannot access these fields directly outside the scope of the class, but we can have properties that access these private domains.

1.49.1. Accessors #

The accessor of a property contains executable statements that help you get (read or evaluate) or set (write) the property. The accessor declaration can contain a get accessor, a set accessor,or both. For example:

// Declare a Code attribute of type string public string Code { get { return code; } set { code = value; } } // Declare the Name attribute of type string public string Name { get { return name; } set { name = value; } } // Declare Age attribute of type int public int Age { get { return age; } set { age = value; } } 

1.49.2. Example #

The following example demonstrates the use of Property:

Example #

using System; namespace runoob { class Student { private string code = "N.A"; private string name = "not known"; private int age = 0; // Declare a Code attribute of type string public string Code { get { return code; } set { code = value; } } // Declare the Name attribute of type string public string Name { get { return name; } set { name = value; } } // Declare Age attribute of type int public int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } } class ExampleDemo { public static void Main() { // Create a new Student object Student s = new Student(); // Set the code, name, and age of the student s.Code = "001"; s.Name = "Zara"; s.Age = 9; Console.WriteLine("Student Info: {0}", s); // Increase age s.Age += 1; Console.WriteLine("Student Info: {0}", s); Console.ReadKey(); } } } 

When the above code is compiled and executed, it produces the following results:

Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10 

1.49.3. Abstract Properties #

Abstract classes can have abstract properties, which should be implemented in derived classes. The following procedure illustrates this:

Example #

using System; namespace runoob { public abstract class Person { public abstract string Name { get; set; } public abstract int Age { get; set; } } class Student : Person { private string code = "N.A"; private string name = "N.A"; private int age = 0; // Declare a Code attribute of type string public string Code { get { return code; } set { code = value; } } // Declare the Name attribute of type string public override string Name { get { return name; } set { name = value; } } // Declare Age attribute of type int public override int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } } class ExampleDemo { public static void Main() { // Create a new Student object Student s = new Student(); // Set the code, name, and age of the student s.Code = "001"; s.Name = "Zara"; s.Age = 9; Console.WriteLine("Student Info:- {0}", s); // Increase age s.Age += 1; Console.WriteLine("Student Info:- {0}", s); Console.ReadKey(); } } } 

When the above code is compiled and executed, it produces the following results:

Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。