C # Class properties and methods

Source: Internet
Author: User

PropertiesThe public class field is equivalent to C # inside exposed to outside properties like Nodejs Module.exports but the property is different from the normal field, the property is only the Outer packing field does not own any content similar to the return method. Or you need an internal field to set it. The private class field is equivalent to the field used inside the class. In fact, you can think of the property as a private variable accessor 1, the role of the property: Protect the field, the value of the field and assign the value of 2, the use of attributes:
_name (property name) public [corresponding field type] property name Name{get{return _name;} Set{_name=value;}}

  

3, field and attribute naming format: The name of the field is generally used: _camecase field naming method generally adopted: Pascalcase 4, after having attributes, often through the property to access the field. Properties are generally declared public, and fields are usually declared private. Fields that are accessed externally in a class are implemented through attributes. Get can define the value, set can define the assignment, usually we refer to get and set as accessors. 5, the attribute is divided into 4 kinds, 1) both read and write: both Get and Set2) read-only: contains only get3) automatic attribute example: only include Set4)
Class Gwyy {private string _name;//field private public string Name{get {return _name + ' a ';} set {_name = value;}} property, public but still need to rely on entity fields}

  

Method: A "method" is a block of code that contains a series of statements. In C #, each execution instruction is done in the context of the method. Method is declared in a class or struct, when declared, you need to specify the access level, return value, method name, and any method arguments. The method arguments are enclosed in parentheses and separated by commas. An empty parenthesis indicates that the method does not require parameters. The following class contains three methods: class Motorcycle {public void StartEngine () {} public void Addgas (int. gallons) {} public int drive (int miles , int speed) {return 0;}} Static methods static methods do not instantiate a particular class, calling a non-static method that requires specifying the class name to be called slightly ... Method overloads method name is the same, but parameter data type, number, order different variable parameters can be added in the method parameter bar params property to accept a one-dimensional array of variable parameters
public void Test1 (params string[] list) {for (int i = 0; i < list. Length; i++) {Console.WriteLine (List[i]);}

  

When ref passes the calling method by reference, any changes made to the parameter in the method are reflected in the variable. Using the ref parameter must be the method declaration and the caller must display the use of the REF keyword
int i = 3; Console.WriteLine (i); G.test2 (ref i); Console.WriteLine (i);p ublic void test2 (ref int i) {i = 55;}

  

The out definition output parameter parameters are passed by reference. Ref requires that a variable be initialized before it is passed. The out parameter is not initialized. Both the declaration and the caller must be displayed using the Out
int Value;g.test3 (out value);p ublic void test3 (out int i) {i = 66;}

  

C # Class properties and methods

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.