C # Basic----> methods

Source: Internet
Author: User

  Public classTree {/*         * 1.0 consider a problem;         * We have a piece of code that needs to be executed in different places once!!!             * * * 2.0 how to Implement * a--> copy and paste * b--> this piece of code into a method that is called once in the two places needed.             * * 3.0 Method * A---> mechanism for a code reuse * B---> is also called as a function in the process the method and function refer to the same thing.         * * 4.0 syntax to create method * Access Modifier method return value method name ([parameter list]) * {* Method body *}  * * About the definition of the method * A.         Define the location of the method: defined directly below the class.  * B. There are 5 types of access modifiers. Public is one of them.  So this place doesn't care what it means, just write public * c.         The Static keyword.          * Our own method of definition is called if required in 1 static decorated methods.         * Then the method we define must be static to be called.  * d.  The return value of the method * E. Name of the method: * 1). The name of the method to be taken is meaningful.         Let others see the name and know what the internal code of the method is doing. * 2).         The code in the method is completed with 1 functions, so the name of the method requires 1 verbs. * 3).         The name of the method is capitalized in the first letter of every 1 words.  * F.         In particular, there is no semicolon behind the parentheses. * * 5. The invocation of the method * Full invocation: Class name. Method name ()----> static method Invocation method, the new class name (). Method name ()----> Non-static method call method adds static to the method;         * If the method is called and the method is defined in the same 1 classes: Method name ();           **/        /// <summary>        ///Public:---> Common///Static : Indicates that the method is a static method///void: Way the method has no return value///Getnum (): The method name of this method, because the parentheses are empty, means that this method is a parameterless method/// </summary>         Public Static voidGetnumone () {//method Body        }        /** Access modifier public Private protected **/        /// <summary>        ///No access modifier is the default private: Indicates that the method is private///int: The method will eventually return a value of type int///getnumtwo (String str) indicates that the passed in parameter is a string of type///Return : Indicates that the data is returned/// </summary>        /// <param name= "str" ></param>        /// <returns></returns>         Static intGetnumtwo (stringstr) {            if(string. IsNullOrEmpty (str)) {return 0; }            Else            {                return 1; }        }          Public stringGetnum () {return "Test"; }        //------------------------------Local variables and member variables--------------------------------------        /*1. Local variables: Variables defined in the method.             * Member variable: A variable directly defined under a class. * * * 2.             The difference between a local variable and a member variable: * A. Scope is different: * Local variables can only be accessed in methods.             * Member variables can be accessed in all methods of this class.             * * Member variable if you want to be accessed in a method that is modified by static.             * then this member variable also needs to be staic modified.             * * B. The default value is different * Local variables are declared without assigning a value to the local variable.             * So you have to make sure that there is a value in this variable before you use it. The member variable has a default value if it is not assigned a value at the time of declaration.                 * If the type of the member variable is of value type then the default value is 0 * If the type of the member variable is a reference type then the default value is NULL * * *              *              */          Public stringName;//think of this definition under the class keyword called member variable          Public voidTestdemo () {intnum =1;//The variable defined in the method is called a local variable.//It's only effective in this method.         }       //------------------------------------------The following sections are property descriptions----------------------------------------        /* 1. Why should I protect a field?         * Because we want to do 1 logical validations when assigning a value to a field or taking a value. * * 2.         Microsoft provides a more concise syntax to protect field---properties.         * The function of the attribute: protect the field. * * 3.         How do I use attributes to protect a field?  * A.         The protected field must be privatized.  * B.               Write 1 properties for this field * syntax: * The name of the Type property of the access modifier property * {* get *         {*} * set * {*} *} * * 4.         External how the property is manipulated.         * A. The icon is a wrench that represents the property.         * B. When an external operation takes an attribute as a field, assign a value directly using an assignment symbol or directly write the name of the property. * * 5. How the property is to protect the field's * a. When assigning a value to a property, the set code block in the property is called.         In set, you can get the value assigned by the user to the property by the keyword value * In general, we check the set code block to verify that the value assigned by the user for the property is logical.         * Assign a user-assigned value to a protected field if logic is met.         * Otherwise do default processing.         * * B. When we take the value of the property, it executes the contents of the Get code block in the property.         * The value that is finally taken is the value returned in the Get block.       * In general, we return the value of the property-protected field in the Get code block.  * * 6. Note the following points when writing attributes * A, access modifiers.          In fact, any can be written, because the property is to be accessed externally. * So in most cases, the access modifier for a property should be public * b, and the type of the attribute cannot be void.         The type of the property is generally the same as the type of the protected field.   * C.         Name of the property: capitalize the first letter of each word.   * The name of the property is arbitrary, but the recommended and protected field names are consistent (case sensitive) for easy code reading * d.         When assigning a value to a property, the type of the data is consistent with the type of the property, regardless of the type of field being protected.         * Of course, in most cases, the type of the property is the same as the type of the protected field.   * E.         The type of value in a set block is determined by the type of the property.   * F. The get code must be quick to return data before the end.         The type of data returned is determined by the type of the property.   * g.         The property can contain only get and set blocks, and other code cannot be written directly into the attribute. * * 7.         Note * A. must have a field first, and then use the property to protect it. * B. Operations in Get and set are fields. Instead of manipulating properties.         If the property is manipulated, a dead loop occurs, * C. The type of the property is in most cases consistent with the protected field, * if you want the outside world to have the same type of value as the field, then it can be inconsistent. * Gender of type bool want to get 1 string type "male" female * d. 1 fields corresponding to 1 properties.         Do not have multiple fields corresponding to 1 properties This is not a good operation.         * E. In the set block, we determine whether value is logical, not the judgment field.         * * F. In the interior of the class. When the value is taken, is the value of the property taken or the value of the field? * 1) If the attribute takesNo logic validation is done for the value.         Directly returns the value of the field * then this time, the value of the property is not the same as the value of the Fetch field, because the values are the same.         * But it is recommended that you take the value of the field because it is more efficient.         * 2) If a property is valued, there is a logic validation. Then this time the value of the property and the value of the Fetch field are not the same.         * Then depending on your specific situation to decide, see what you want to get the value. * * when assigning values.         If the property is assigned a logical process. The assignment to a field at this time is not the same as assigning a value to a property.         * If you want your value to be very special do not need to be verified then directly assign to the field * if you want your value to be verified then assign to the attribute. * * 8.         The nature of the property is 1 get methods and a set method.             * But Microsoft provides 1 more concise ways to help write these two methods * We can then set the value of the key through the value of the user-assigned values because set is essentially 1 methods The parameter of this method is called value * * * 9.         Shortcut keys for quick encapsulation of properties for a field: ctrl+r+e * * 10.c# Specification for attributes: * As long as the field needs to be accessed by the outside world. The attribute should be encapsulated for this field, regardless of whether there is logical validation at the time of value or assignment. * * * 11.  The write-only attribute * property has only a set block and no get block. You can only assign a value to this property.         The value of this property cannot be taken.         * Read-only attribute * Property only get block without set block, then can only take the value of this property and cannot assign a value to this property.         * * attribute must have at least 1 accessors: * * 12.     We assign the value to the property, and we think the value is stored in the attribute.    * This is not the case * property is not able to store data.         We assign a value to the property and then the set block of the property stores the value in the field.           * * Only variables in C # can store data. * * 12. Automatic Properties * If the property is encapsulated in a field that is readable and writable.         And there is no logic validation at the time of reading and writing.         * So this time, we can write 1 automatic attributes without writing a paragraph.         * * When we write 1 automatic genera, the C # compiler will automatically produce 1 fields when compiling.         * This field is then encapsulated by the automatic attribute.         * * When can I write 1 automatic attributes: * A. Readable and writable.               * B. No logic verification when reading and writing **/          Public Doubleweight;  Public DoubleWeight {Get;Set; } Private intAge ;  Public intAge {Get             {                 return  This. Age; }             Set             {                  This. Age = Value >=0&& value <= -? Value: -; }         }    }

The following is a demo demo using the console program

 classProgram {Static voidMain (string[] args) {                        //calling a static methodTree.getnumone (); Tree Tree=NewTree (); Console.WriteLine (tree.   Getnum ()); //print out the tree. The return value of Getnum () tree. Getnum () is an instance method, so create an instance of the tree first//Using AttributesConsole.WriteLine (tree. Age);//print out the value of the Age propertyConsole.readkey (); }          }

C # Basic----> methods

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.