C # Tutorial C # Properties

Source: Internet
Author: User
Tags tutorialspoint

C # attribute (property)

property is a named (named) member of the Class (class), struct (structure), and interface (interface). A member variable or method in a class or struct is called a domain (field). The property is an extension of the field (field) and can be accessed using the same syntax. They use accessors (accessors) to allow the value of a private domain to be read or written or manipulated.

property does not determine where the storage is stored. Instead, they have accessors (accessors) that read and write or calculate their values.

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

Accessor (accessors)

The accessor (accessor) of the property contains an executable statement that helps to get (read or evaluate) or set (write) the property. An accessor (accessor) declaration can contain a get accessor, a set accessor, or both. For example:

The Code property that declares the type string is public string code{   get   {      return Code;   }   Set   {      code = value;   }} The Name property of the declaring type string is public string name{   get   {     return Name;   }   Set   {     name = value;   }} The age property of the declaring type int is public int age{    get   {      return age;   }   Set   {Age      = value;   }}

Instance

The following example shows the use of the property:

Using System;namespace tutorialspoint{class Student {private String code = "N.a";      private string name = "not known";      private int age = 0;         The Code property that declares the type string is public string code {get {return Code;         } set {code = value;         }}//declares the Name property of type string to public string name {get {return Name;         } set {name = value;         }}//declares an age property of type Int. public int: {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 = NE                     W Student (); Set student's code, NAME and age 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 = 9Student Info:code = 001, Name = Zara, age = 10

Abstract properties

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

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 = "N.a";      private string name = "N.a";      private int age = 0;         The Code property that declares the type string is public string code {get {return Code;         } set {code = value;  }}//declaration type is string Name property public override string name {get {return         Name         } set {name = value;         }}//declares an age property 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 S                     Tudent ();         Set the code, name, and age S.code = "001" for student;         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 = 9Student Info:code = 001, Name = Zara, age = 10

The above is the "C # Tutorial" C # Property content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.