Latest web development tutorials

C # attributes (Property)

Property (Property) is a class (class), structure (structure) and interface (interface) named (named) members.Class or struct member variable or method called thedomain (Field).Property (Property) is a domain (Field) extension and can be accessed using the same syntax. They useaccessors (accessors) allows the value of a private field can be read or operations.

Property (Property) does not determine storage location. Instead, they have a read-write or calculate their valueaccessor (accessors).

For example, there is a class called Student with age, name and code of the private domain. We can not directly access these fields outside the scope of the class, but we can have access to these private properties field.

Accessor (Accessors)

Property (Property)accessors (accessor) contains information to help get (read or calculated) or set (write) attribute executable statement.Accessors (accessor) statement may contain a get accessor, a set accessor, or contain both. E.g:

// Declare properties of type string public string Code of Code
{
   get
   {
      return code;
   }
   set
   {
      code = value;
   }
}

// Declare the Name attribute of type string public string Name
{
   get
   {
     return name;
   }
   set
   {
     name = value;
   }
}

// Declare properties of type int public int Age of Age
{ 
   get
   {
      return age;
   }
   set
   {
      age = value;
   }
}

Examples

The following example demonstrates the property (Property) Usage:

using System;
namespace tutorialspoint
{
   class Student
   {

      private string code = "NA";
      private string name = "not known";
      private int age = 0;

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

      // Declare properties of type int public int Age of 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 student's code, name and age
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine ( "Student Info: {0}", s);
         // Increasing 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

Abstract properties (Abstract Properties)

An abstract class can have abstract properties that should be implemented in a derived class. The following program illustrates this point:

using System;
namespace tutorialspoint
{
   public abstract class Person
   {
      public abstract string Name
      {
         get;
         set;
      }
      public abstract int Age
      {
         get;
         set;
      }
   }
   class Student: Person
   {

      private string code = "NA";
      private string name = "NA";
      private int age = 0;

      // Declare properties of type string public string Code of 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 properties of type int public override int Age 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 student's code, name and age
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine ( "Student Info: - {0}", s);
         // Increasing 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