C # Fundamentals (iii), properties

Source: Internet
Author: User

1. Properties: Improve security

(1) Composition: Access modifier return Type property name

get{statement Collection;} Read properties, which can only be retrieved when they exist alone

set{statement Collection;} Write attributes, which can only be assigned when they exist alone

(2) static property: Access modifier static return type property name

get{statement Collection;} Read properties, which can only be retrieved when they exist alone

set{statement Collection;} Write attributes, which can only be assigned when they exist alone

Static properties can only access static members of a class and apply to an entire class rather than to another instance

(3) Adding a checksum code during a retrieval or assignment can delay instantiation

public static Double

{

Get

{

return age;

}

Set

{

Validating data

if (value< 0)

{

Console.WriteLine ("Age cannot be negative");

Return

}

Else

{

Age= value;

}

}

}

Public double Balance

{

Get

{

if (_balance < 0)

Console.WriteLine ("No Available Balance");

return _balance;

}

}

    

C # Fundamentals (iii), properties

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.