The encapsulation of Basic Learning C #

Source: Internet
Author: User
When it comes to encapsulation, it is actually the problem of the basic class, which provides the system and the system, the module and the module, the realization means of the interaction between the class and the class.

As a junior GIS programmer, the concept of packaging those macro-based, not to mention, programming often face is "field, properties, methods," which is one of the basic object-oriented concepts.

1. Fields

Typically defined as private, which represents the state information of a class

private string name;

2. Properties

Usually defined as public, representing the external members of the class. Properties are readable, writable, and read-write control is implemented through get and set accessors. If the property is read-only, only the get accessor is implemented, and if the property is writable, the set accessor is implemented. There is also a parameter-containing attribute, called an indexer in C #. Indexers are typically used to facilitate the reference to a class instantiation object.

The code is as follows:

public string Name{get{return Name;} Set{name=value==null? String.empty:value;//name?? String.Empty (null on the left, return to the right operand value, not NULL to return to left operand)}}

In fact, this is directly in the VS2010 is intelligent, check the field → Select the refactoring → package field, so OK.

3. Methods

The method encapsulates the behavior of the class and provides the external representation of the class. Used to provide external interfaces with the internal details of the encapsulation as public methods, and the method of external interaction is usually implemented as publicly. But the operation inside the class is basically implemented by private way, which guarantees the hiding and protecting of internal data. In VS2010, you can also select the code snippet → select Refactor → extract method.

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.