C # Properties and variables learn differently

Source: Internet
Author: User

Refer to a three-layer architecture yesterday (not how to understand) found that there is usually a page of code dedicated to the user to store the field and all are encapsulated with attributes. What surprises me is that many field properties exist get{} and set{} and normal variables are no different (readable writable) I just feel that the attribute is to give the field a more choice, and some fields are not allowed to change.

Blog Park A check, and really rose to see

When you write a program, you always feel that a field will satisfy your requirements, so why use a property that is problematic to define.
To get, set to define, the code is large, I really do not know their differences and advantages
Cases:

public class MyClass
{

public static string A

Private Sting _b= "";
public string Getval
{
get {Retrun _b;}
set {_b = value;}
}
}

MyClass. A and MyClass. Getval may implement the same function, why do you have to define attributes?

Now briefly summarize the advantages of attributes over common variables:
1. Data security
Because A is a public variable, it can be arbitrarily changed outside the class, in order to prevent arbitrary changes by the user, the available properties get, set to limit _b's read-only, write-only, or control the maximum or minimum of _b
Quote a man's Jane answer: "In fact, the custom variable has been implemented by default properties, but simply read and write, we define the attributes to read and write on the basis of adding more restrictions, later you learn more will find that a lot of things are for data security to join, plainly is the limit, such as interface, The method in the interface you must (note is mandatory) to implement the "
2. One of the principles of OOP and programming is that data encapsulation means that we should not provide the type of the field to the outside world in a public way
3, in fact, simply write a program is not difficult, rare is how to design the program, so that the code is simple and easy to read and efficient and the highest, how to ensure the safe operation of the program

has been useless in form forms using these properties virtual methods abstract class interfaces such as: How to use it in the program ...

After two weeks of class set up, to find ways to add to the study of these. Add the application to the form in C # advanced programming. Will reap a lot of it huh

Reference two

A class, sometimes it is unclear whether a member variable or a property is used.
Such as:
Member variables
public string Name;
or with attributes
private string Name
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}

Properties are similar to member variables, they all provide data storage, but the functionality of properties is far more powerful than member variables. Properties are accessed by special methods (Get and Set accessors). Get and set accessors allow you to validate property values, execute other code, or perform other tasks after setting or retrieving properties

For example

Member variables are written like this
Public ReadOnly string Name;
Well, it could be a read-only.

private string Name
public string Name
{
Get
{
return name;
}

}

The object-oriented programming approach is to abstract, encapsulate, and in a class, the variables defined are positive to the class itself, and we call them domain. It can be public, private, etc. attributes are attributes of the class that are seen externally, and are attributes that the class presents to the external consumer. As we mentioned earlier, the domain can be public, but declaring the domain as public will be detrimental to the encapsulation of the class, because the external consumer can modify the class directly. So we can use properties, we just expose their properties, and how to assign (set) how to take a value (get) has been encapsulated, outside of the class is not visible. It can only be used by external users, cannot control it, and how to control operations is determined by the class itself. Do you understand?

Reference Three

The difference between C # variables, fields, properties, methods

String name1;//variable that can appear anywhere in need of him

public class MyClass//Class

{

public string name2;//field, member variable. is a variable, but in the MyClass class, it is called a field, or it can be called a member variable

private string name3;//, which is also a variable, adds the private modifier to the

private string name= "";//Mate property to use

public string name//Property

{

get {return name;} Get accessor

set {name=value;} Set accessor

}

public string Name_fangfa ()//method

{

return name;

}

}

Bottom line: Fields, properties are variables, just to differentiate and data security settings.

Usage scenarios for fields: closely related to classes or objects, it is recommended to use the private adornment.

Attribute usage Scenario: encapsulates the field and provides access to the Get/set keyword.

Usage scenarios for variables: not closely related to classes or objects, often used in methods or block of statements.

Fields and properties are relative to the class, and variables can be used anywhere relative to the method or block of statements.

C # Properties and variables learn differently

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.