Don't confuse several small concepts in C #.

Source: Internet
Author: User

When I wrote a blog post about WCF contract, I suddenly found that there are several small concepts of C # that need to be mentioned. Otherwise, it is easy to confuse others. Therefore, this article is listed separately.

Group 1: attribute, property, Field

Many people think this group of concepts is a concept. Actually, in C #, they refer to completely different content.

 1     [Serializable] 2     class Person 3     { 4         private string name;      5  6         public string Name 7         { 8             get 9             {10                 return this.name;11             }12             set13             {14                 if (!string.IsNullOrEmpty(value))15                 {16                     this.name = value;17                 }18             }19 20         }21         22         public int Age { get; set; }23        24     }

Note: The first line: [] is the attribute in brackets. How can this problem be solved? Someone translates it into "Features ". I think it's better not to translate it, but also to call it attribute. Attribute is used to modify class, interface, and method. It is placed on the last line defined by class, interface, and method. Serializable is a defined "Attribute Class" in the system. It modifies other classes, interfaces, and methods. [Serializable] modifying a class indicates that the new object (object) of the class can be serialized into XML. XML can be deserialized into an object ). You can define your own "Attribute Class" to modify other classes/interfaces. It is not mentioned here, as long as you know that attribute is located in the brackets of the line above the class.
Row 4: Private string name; here name is field. Someone translates it into a field ". I personally think it is inappropriate to keep it as field. It is actually the so-called private variable in C ++. Field is private, so it cannot be accessed outside the class.

Rows 6 to 20th are called property. Someone translates it into "attribute ". I still think that the property is retained. Property provides an easy way to access the field. Simple filter conditions can be implemented here. If the property body only has get {} but no set {}, the property/field is read-only.

Row 22nd: in general, we define a property for filed to facilitate external access. C # also allows us to define property without having to write field, such as age. During compilation, the CLR will automatically generate the field for us.

Group 2: Function & method (Operation)

Generally, functions are translated into "functions" and methods are translated into "methods", right?

I think the main difference is that they are differentiated from each other. script languages such as C, Perl, PHP, Python, powershell, and Unix bash seem to use many functions. in C #, Java and other languages, method is used a lot. personal feeling

A function does not need to depend on a class or an object. It is relative to a common statement in a script (a statement not extracted as a function;

Method depends on a class/object, which is relative to the property in the class.

Operation I think it is a concept with method, and it is an operation on an object.

========================================================== ========================================================== =====

Write this first, and then append it when you need to clarify the concept separately.

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.