1.49. C# property

发布时间 :2023-10-12 23:00:07 UTC      

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
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.