C # Get and set usage

Source: Internet
Author: User

C # Get set is easy to use, but you must have a good understanding of the basics to better understand C # Get set usage.

In C # Get set usage, get is the operation for reading the attribute, and set is the operation for setting the attribute. Define an attribute. If only get is available, this attribute is read-only. Similarly, only set attributes are written only. Of course, writing only attributes have no meaning for tasks.

Assume that a bank can save money and withdraw money.

1. Private m_money;

2. Private class bank ()

3 .{

4. Get

5 .{

6. Return m_money;

7 .}

8. Set

9 .{

10. m_money = Value

11 .}

12 .}

Money is like an ATM in a bank. You can't see the money in it, but you can use set to save money and get to get money ). M_money is a private field and is encapsulated in a class. Programs other than the class cannot directly access it. in C # Get set usage, the set and get members of the class are the only way to get the internal properties of the external program, just as you go to the bank to get the money, you cannot get the money directly from the bank's safe, instead, the Bank operator gives you the money.

Attributes look like a common variable to the caller. If a common variable is used, you can use it. However, as a class designer, you can use attributes to hide some fields in your class, this allows the outside world to access your fields only through attributes. You can use attributes to restrict external access to your fields and use get, set, if you want users to access your fields at will, set and get are implemented. If you only want users to read fields, only get is implemented, if you only want the user to write segments, you can only implement set. You can also perform some verification on the values passed by the user in set and get, make sure that your field contains the correct value.

C # Get set usage example

13. Private int;

14. Public int Index

15 .{

16. Get

17 .{

18. Return;

19 .}

20. Set

21 .{

22. If (value> 0)

23. A = value;

24. Else

25. A = 0;

26 .}

27 .}

We can see that C # Get Set has a function feature.

C # Get set is used to hide real members inside a component or class,
Second, it is used to establish constraints, for example, implementing the constraint "I don't have you.
The third is to respond to the attribute change event. When the attribute changes, you only need to write it in the set method.

There are two ways to reveal the naming attributes of a class-either through a domain member or through a property. The former is implemented as a member variable with public access; the latter does not directly respond to the storage location, but is only accessed through the accessors.

When you want to read or write the attribute value, the access flag limits the implemented statement. The access Mark used to read the attribute value is recorded as the keyword get, and the read/write mark used to modify the attribute value is recorded as set.

28. Using system;

29. Public class house

30 .{

31. Private int m_nsqfeet;

32. Public int squarefeet

33 .{

34. Get

35. {return m_nsqfeet ;}

36. Set

37. {m_nsqfeet = value ;}

38 .}

39 .}

40. Class testapp

41 .{

42. Public static void main ()

43 .{

44. House myhouse = new house ();

45. myhouse. Fig = 250;

46. Console. writeline (myhouse. squarefeet );

47 .}

48 .}

The house class has an attribute named squarefeet, which can be read and written. The actual value is stored in a variable that can be accessed from the class. If you want to rewrite it as a domain member, all you need to do is to ignore the access Mark and redefine the variable:

49. Public int squarefeet;

This is good for such a simple variable. However, if you want to hide the details of the class's internal storage structure, you should use an access flag. In this case, the Set access flag transmits a new value to the attribute in the value parameter. (You can change the name. For more information, see row 10th .)

In addition to hiding implementation details, you can also freely limit various operations:
Get and set: allow read/write access to properties.
Get only: Read attribute values only.
Set only: only attribute values can be written.
In addition, you can get the opportunity to implement valid code in the Set flag. For example, for various reasons (or no reason at all), you can reject a new value. It is better that no one tells you that it is a dynamic attribute-when you request it for the first time, it will be saved, so we should postpone resource allocation as much as possible.

The above is a brief introduction to the usage of C # Get set.

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.